Set Methods
Part of the Logic & Flow section of Coddy's Python journey — lesson 31 of 78.
Here are even more essential set methods:
discard(element): Removes the specified element from the set if it exists. Does not raise an error if the element is not found.
my_set = {1, 2, 3}
my_set.discard(3) # Removes 3 from the set
my_set.discard(4) # No error, even though 4 is not in the set
print(my_set)
# Output: {1, 2}pop(): Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.
my_set = {1, 2, 3}
element = my_set.pop() # Removes and returns an arbitrary element
print(element)
# Output: 1 (or another element, as it's arbitrary)
print(my_set)
# Output: {2, 3} (or a different set, depending on the popped element)clear(): Removes all elements from the set, making it empty.
my_set = {1, 2, 3}
my_set.clear() # Removes all elements
print(my_set)
# Output: set()Challenge
EasyWrite a program that performs the following tasks:
- Create a set called
numberscontaining the values 10, 20, 30, 40, 50. - Use the
discard()method to remove 35 from the set. - Use the
pop()method to remove an arbitrary element and store it in a variable calledpopped_element. - Use the
clear()method to empty the set. - Print the following:
- The set after using
discard(). - The value of
popped_element. - The set after using
clear().
- The set after using
Cheat sheet
Essential set methods for removing elements:
discard(element): Removes the specified element from the set if it exists. Does not raise an error if the element is not found.
my_set = {1, 2, 3}
my_set.discard(3) # Removes 3 from the set
my_set.discard(4) # No error, even though 4 is not in the setpop(): Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.
my_set = {1, 2, 3}
element = my_set.pop() # Removes and returns an arbitrary elementclear(): Removes all elements from the set, making it empty.
my_set = {1, 2, 3}
my_set.clear() # Removes all elements
print(my_set) # Output: set()Try it yourself
# Write code hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter