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 discarded = 'hello'
Output:
The given set is : {'is', 'this', 'hello', 'btechgeeks'} The given set after discarding the given specific item : {'is', 'this', 'btechgeeks'}
Example2:
Input:
Given set = {10, 20, 30, 40, 50, 60, 70} item to be discarded = 90
Output:
The given set is : {70, 40, 10, 50, 20, 60, 30} The given set after discarding the given specific item : {70, 40, 10, 50, 20, 60, 30}
Explanation:
Here there is no 90 in the given set. so, discard() method prints the given set itself without raising an error. whereas the remove() method raises an error if item is not present.
Program for Set discard() Method in Python
Set discard() Method:
The discard() method deletes the item specified from the set.
This method differs from remove() in that the remove() method will throw an error if the specified item does not exist, whereas the discard() method will not.
Syntax:
set.discard(value)
Parameters:
value: This is required.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 discarded as static input and store it in another variable.
- Apply discard() method to the given set which discards the above given specific item from the given set.
- Print the above-given set after discarding 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 discarded as static input. # Store it in another variable. discrd_item = 'hello' # Apply discard() method to the given set which discards the above given specific item # in the given set. gven_set.discard(discrd_item) # Print the above-given set after discarding the given specific item. print("The given set after discarding the given specific item :") print(gven_set)
Output:
The given set is : {'is', 'this', 'hello', 'btechgeeks'} The given set after discarding the given specific item : {'is', 'this', 'btechgeeks'}
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 discarded as user input using the int(input()) function.
- Store it in another variable.
- Apply discard() method to the given set which discards the above given specific item from the given set.
- Print the above-given set after discarding 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 discarded as user input using the int(input()) function. # Store it in another variable. discrd_item= int(input("Enter some random number = ")) # Apply discard() method to the given set which discards the above given specific item in the given set. gven_set.discard(discrd_item) # Print the above-given set after discarding the given specific item. print("The given set after discarding the given specific item :") print(gven_set)
Output:
Enter some random values separated by spaces = 10 1 4 5 2 1 3 2 4 The given set is : {1, 2, 3, 4, 5, 10} Enter some random number = 5 The given set after discarding the given specific item : {1, 2, 3, 4, 10}
Well organized and easy to understand tutorials of us containing Python Set Methods Examples will help you work effectively on Python Set Methods.