Introduction to Python Set Methods
Set is one of the important concepts in Python. Set is an unordered and unindexed collection in Python, usually represented within curly brackets. Python is one of the coding languages that has a towering presence in the field of Machine learning and data science. Python is so vast that getting a grip on its every aspect is very difficult.
Example:
Fruits_set = {Apple,Banana,Litchi}
Methods of set in Python
Now let’s see methods related to set:
1. copy()
This method copies one set to other sets. This method doesn’t require any parameters.
Code:
Veggie_set = {"Drumstick","Cabbage","Cauliflower"}
type(Veggie_set)
new_set = Veggie_set.copy()
print(new_set)
type(new_set)
Output:
2. add()
This method adds a new element to the list. The new element needs to specified as a parameter in add().
Code:
Veggie_set = {"Drumstick","Cabbage","Cauliflower"}
type(Veggie_set)
Veggie_set.add("Lady-finger")
print(Veggie_set)
Output:
As one sees “Veggie_set.add(“Lady-finger”)” this helps in adding the element “Lady-finger” in the list name “Veggie_Set”.
3. clear()
This method helps in clearing the set. No element remains in the set after this method is applied.
Code:
Veggie_set = {"Drumstick","Cabbage","Cauliflower"}
print(Veggie_set)
Veggie_set.clear()
print(Veggie_set)
Output:
4. difference()
Code:
Veggie_set1 = {"Drumstick","Cabbage","Cauliflower"}
Veggie_set2 = {"Brocoli","Corn","Cabbage"}
New_set = Veggie_set1.difference(Veggie_set2)
print(New_set)
Output:
As one can notice, “Cabbage” has been removed when “Veggie_Set1.difference(Veggie_Set2)”. The same can be done the other way:
Code:
Veggie_set1 = {"Drumstick","Cabbage","Cauliflower"}
Veggie_set2 = {"Brocoli","Corn","Cabbage"}
New_set = Veggie_set2.difference(Veggie_set1)
print(New_set)
Output:
The same element which is present in both, i.e. “Cabbage”, has been removed.
5. dicard()
This method helps in the removal of an element from the set.
Code:
Veggie_set1 = {"Drumstick","Cabbage","Cauliflower"}
print(Veggie_set1)
Veggie_set1.discard("Cauliflower")
print(Veggie_set1)
Output:
Suppose one needs to discard more than one element from the set. A new set can be formed with all elements to be removed, and then the difference() method can be used.
Code:
Veggie_set1 = {"Drumstick","Cabbage","Cauliflower"}
print(Veggie_set1)
set_to_remove = {"Cauliflower","Cabbage"}
New_set = Veggie_set1.difference(set_to_remove)
print(New_set)
Output:
6. intersection()
This method helps in finding the common elements among more than two sets.
Code:
Veggie_set1 = {"Drumstick","Cabbage","Cauliflower"}
print(Veggie_set1)
Veggie_set2 = {"Cauliflower","Cabbage","Lady-finger"}
print(Veggie_set2)
New_set = Veggie_set1.intersection(Veggie_set2)
print(New_set)
Output:
As one can notice, how the common elements are taken out by the function intersection(). Common elements printed are “Cabbage” and “Cauliflower”, which returned in the form of a set. If one has to take an intersection between more than one set, this is how it can be done:
Code:
Veggie_set1 = {"Drumstick","Cabbage","Cauliflower"}
print(Veggie_set1)
Veggie_set2 = {"Cauliflower","Cabbage","Lady-finger"}
print(Veggie_set2)
Veggie_set3 = {"Cauliflower","Spinach","Kale"}
print(Veggie_set3)
New_set = Veggie_set1.intersection(Veggie_set2,Veggie_set3)
print(New_set)
Output:
Syntax:
set1.intersection(set2, set3 ...etc.)
More than one set can be put in parameters of the intersection.
7. Issubset()
This method helps you identify if the set is a subset of others or not. The method issubset() always return Boolean output. It will return “True” if all elements of one set exist in a specified set; else, it will return “False.”
Code:
Veggie_set1 = {"Cabbage","Cauliflower"}
print(Veggie_set1)
Veggie_set2 = {"Cauliflower","Cabbage","Lady-finger"}
print(Veggie_set2)
Eval = Veggie_set1.issubset(Veggie_set2)
print(Eval)
Output:
As one can notice, “Veggie_set1” is actually the part of “Veggie_set2”. Hence issubset() method is returning True.
8. issuperset()
This method helps you identify if elements of a set are part of other specified sets. This gives output in the form of True or False.
Code:
Veggie_set1 = {"Cabbage","Cauliflower"}
print(Veggie_set1)
Veggie_set2 = {"Cauliflower","Cabbage","Lady-finger"}
print(Veggie_set2)
check1 = Veggie_set1.issuperset(Veggie_set2)
print(check1)
check2 = Veggie_set2.issuperset(Veggie_set1)
print(check2)
Output:
As one can notice, all elements of “Veggie_set1” are part of “Veggie_set2”. That means “Veggie_set2” is a superset of “Veggie_set1” Hence:
Statement “Veggie_set1.issuperset(Veggie_set2)” returns False.
Statement “Veggie_set2.issuperset(Veggie_set1)” returns True.
9. symmetric_difference()
This method helps you find the elements that are part of sets but not common to both sets.
Code:
Veggie_set1 = {"Cabbage","Cauliflower","Kale"}
print(Veggie_set1)
Veggie_set2 = {"Cauliflower","Cabbage","Lady-finger"}
print(Veggie_set2)
check = Veggie_set2.symmetric_difference(Veggie_set1)
print(check)
Output:
If one notices carefully, elements “Cabbage” and “Cauliflower” are part of both sets. Hence. When we do symmentric_difference() between both sets, it will result in “Kale” and “Lady-finger”, where “Kale” is part of only “Veggie_set1” and “Lady-finger” part of only “Veggie_part2”.
10. union()
This method helps you unite elements from two sets. The common and the uncommon elements from both sets are formed as a new set.
Code:
Veggie_set1 = {"Cabbage","Cauliflower","Kale"}
print(Veggie_set1)
Veggie_set2 = {"Cauliflower","Cabbage","Lady-finger"}
print(Veggie_set2)
check = Veggie_set2.union(Veggie_set1)
print(check)
Output:
As one can notice, “check” is the new set formed after the union of two sets “, Veggie_set1” and “Veggie_set2”. Common elements are: “Cabbage”, ”Cauliflower”. Uncommon elements are: “Kale”, ”Lady-finger.”
All these common and uncommon elements are part of this new set “check”, which is the union of both sets. For more than one set, it can be like:
Set1.union(set2,set3..etc.)
11. update()
This method helps update a set with the element that is part of other specified sets but not of this set.
Code:
Veggie_set1 = {"Cabbage","Cauliflower","Kale"}
print(Veggie_set1)
Veggie_set2 = {"Cauliflower","Cabbage","Lady-finger"}
print(Veggie_set2)
Veggie_set2.update(Veggie_set1)
print(Veggie_set2)
Output:
One can see, “Kale” which was part of “Veggie_set1”. But after performing update() over “Veggie_set2” with “Veggie_set1”, “Kale” became an element of “Veggie_set2”.
Conclusion
As we saw above, how important is the concept of set is and hence knowing its method helps you get a good understanding and grip over the set? The above-discussed methods are built-in methods in Python.
Recommended Articles
This is a guide to the Python Set Methods. Here we discuss the basic concept and methods of set in Python with code and output. You may also have a look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses