Set Methods
What You'll Learn: In this tutorial, you'll discover various built-in methods that you can use on sets in Python. These methods help you manage and manipulate set items efficiently.
Set Methods and Descriptions
add()- Description: Adds an element to the set.
Example Code:
pythonmyset = {"apple", "banana"} myset.add("cherry") print(myset)
clear()- Description: Removes all the elements from the set.
Example Code:
pythonmyset = {"apple", "banana", "cherry"} myset.clear() print(myset)
copy()- Description: Returns a copy of the set.
Example Code:
pythonmyset = {"apple", "banana", "cherry"} newset = myset.copy() print(newset)
difference()- Description: Returns a set containing the difference between two or more sets.
Example Code:
pythonset1 = {"apple", "banana", "cherry"} set2 = {"banana", "cherry", "date"} result = set1.difference(set2) print(result)
difference_update()- Description: Removes the items in this set that are also included in another, specified set.
Example Code:
pythonset1 = {"apple", "banana", "cherry"} set2 = {"banana", "cherry", "date"} set1.difference_update(set2) print(set1)
discard()- Description: Removes the specified item.
Example Code:
pythonmyset = {"apple", "banana", "cherry"} myset.discard("banana") print(myset)
intersection()- Description: Returns a set that is the intersection of two or more sets.
Example Code:
pythonset1 = {"apple", "banana", "cherry"} set2 = {"banana", "cherry", "date"} result = set1.intersection(set2) print(result)
intersection_update()- Description: Removes the items in this set that are not present in other, specified sets.
Example Code:
pythonset1 = {"apple", "banana", "cherry"} set2 = {"banana", "cherry", "date"} set1.intersection_update(set2) print(set1)
isdisjoint()- Description: Returns whether two sets have an intersection or not.
Example Code:
pythonset1 = {"apple", "banana"} set2 = {"cherry", "date"} result = set1.isdisjoint(set2) print(result)
issubset()- Description: Returns whether another set contains this set or not.
Example Code:
pythonset1 = {"apple", "banana"} set2 = {"apple", "banana", "cherry"} result = set1.issubset(set2) print(result)
issuperset()- Description: Returns whether this set contains another set or not.
Example Code:
pythonset1 = {"apple", "banana", "cherry"} set2 = {"apple", "banana"} result = set1.issuperset(set2) print(result)
pop()- Description: Removes an element from the set.
Example Code:
pythonmyset = {"apple", "banana", "cherry"} myset.pop() print(myset)
remove()- Description: Removes the specified element.
Example Code:
pythonmyset = {"apple", "banana", "cherry"} myset.remove("banana") print(myset)
symmetric_difference()- Description: Returns a set with the symmetric differences of two sets.
Example Code:
pythonset1 = {"apple", "banana", "cherry"} set2 = {"banana", "cherry", "date"} result = set1.symmetric_difference(set2) print(result)
symmetric_difference_update()- Description: Inserts the symmetric differences from this set and another.
Example Code:
pythonset1 = {"apple", "banana", "cherry"} set2 = {"banana", "cherry", "date"} set1.symmetric_difference_update(set2) print(set1)
union()- Description: Returns a set containing the union of sets.
Example Code:
pythonset1 = {"apple", "banana", "cherry"} set2 = {"banana", "cherry", "date"} result = set1.union(set2) print(result)
update()- Description: Updates the set with the union of this set and others.
Example Code:
pythonset1 = {"apple", "banana", "cherry"} set2 = {"banana", "cherry", "date"} set1.update(set2) print(set1)
Try It Yourself: Fun Exercises
- Add New Fruits:
- Create a set of fruits.
- Use the
add()method to add a new fruit.
- Compare Your Favorite Foods:
- Make two sets of your favorite foods and your friend's favorite foods.
- Use the
intersection()method to find common foods.
- Combine Hobbies:
- Write sets of hobbies for you and your sibling.
- Use the
union()method to combine them into one set.
Summary:
In this Python tutorial, we learned about various set methods including add(), clear(), copy(), difference(), intersection(), union(), and more. These methods help you manage and manipulate sets effectively. Keep experimenting and have fun with sets in Python!