Python Program for Set remove() Method

In the previous article, we have discussed Python Program for Set min() Method
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given set = {'hello', 'this', 'is', 'btechgeeks'}
item to be removed = 'hello'

Output:

The given set is :
{'hello', 'btechgeeks', 'is', 'this'}
The given set after removing the given specific item :
{'btechgeeks', 'is', 'this'}

Example2:

Input:

Given set = {10, 30, 50, 20, 30, 40, 60}
item to be removed = 10

Output:

The given set is :
{40, 10, 50, 20, 60, 30}
The given set after removing the given specific item :
{40, 50, 20, 60, 30}

Program for Set remove() Method in Python

set remove() Method:

In Python, the built-in method remove() removes the element from the set only if the element is present in the set, similarly to the discard() method, but If the element is not found in the set, an error or exception is thrown.

Syntax:

set.remove(item)

Parameter Values : 

item: This is required compulsorily.The item to look for and remove

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Give the item to be removed as static input and store it in another variable.
  • Apply remove() method to the given set which removes the above given specific item from the given set.
  • Print the above-given set after removing the given specific item.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {'hello', 'this', 'is', 'btechgeeks'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Give the item to be removed as static input.
# Store it in another variable.
remve_item = 'hello'
# Apply remove() method to the given set which removes the above given specific item
# in the given set.
gven_set.remove(remve_item)
# Print the above-given set after removing the given specific item.
print("The given set after removing the given specific item :")
print(gven_set)

Output:

The given set is :
{'hello', 'btechgeeks', 'is', 'this'}
The given set after removing the given specific item :
{'btechgeeks', 'is', 'this'}

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Give the item to be removed as user input using the int(input()) function.
  • Store it in another variable.
  • Apply remove() method to the given set which removes the above given specific item from the given set.
  • Print the above-given set after removing the given specific item.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(),map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Give the item to be removed as user input using the int(input()) function.
# Store it in another variable.
remve_item= int(input("Enter some random number = "))
# Apply remove() method to the given set which removes the above given specific item in the given set.
gven_set.remove(remve_item)
# Print the above-given set after removing the given specific item.
print("The given set after removing the given specific item :")
print(gven_set)

Output:

Enter some random values separated by spaces = 10 30 50 20 30 40 60
The given set is :
{40, 10, 50, 20, 60, 30}
Enter some random number = 10
The given set after removing the given specific item :
{40, 50, 20, 60, 30}

Python Set Methods Examples: Understanding Python Set Methods without any practice is a bit difficult but going through this Python Set Methods Examples tutorial will definitely aid programmers to learn easily & efficiently.