The set in python is a collection of unique elements that are unordered and don’t have duplicate elements. These sets can be helpful in many situations, and python developers have created methods for it to extend its functionalities. This article will help you learn the most valuable and used methods in this variable type.
1. Add() Method
The purpose of this method is obvious. It will add the given elements to the set, and you need only specify the element you need to add. Let’s see an example:
# Create a Set
capitals = {"Berlin", "London"}
# Add an Element
capitals.add("Paris")
# Print The Result
print(capitals)
When I run the code on my machine, the add() method adds the element at the beginning of the set, but it will randomly add it to any position. If the element is already there, the add() method won’t add it to it.
2. Clear() Method
The name will tell you that this clear() method will remove all of the elements from your set. Let me show you an example:
# Create a Set
capitals = {"Berlin", "London"}
# Remove The Elements
capitals.clear()
# Print The Result
print(capitals)
When you execute the code, you will see an empty set which means the clear() method has removed all the elements.
3. Copy() Method
This copy() method is straightforward and doesn’t need any parameters because it will only copy the set to another variable. Let’s see an example:
# Create a Set
capitals = {"Berlin", "London"}
# Copy The Set
capitals2 = capitals.copy()
# Print The Result
print(capitals2)
You can use this method when you need to use the set but don’t want to make changes to the original one.
4. Difference() Method
This difference() method will compare two sets and return the items that exist in one set but not the other one. Let me show you an example:
# Create Two Sets
capitals = {"Berlin", "London"}
capitals2 = {"Berlin", "Paris"}
# Compare The Two Sets
result = capitals.difference(capitals2)
# Print The Result
print(result)
The method will return only the elements in the first set, but it doesn’t exist in the second one since we’ve applied the difference() method to the first set.
5. Remove() Method
The good thing about python language is that its command expresses what every function does. This remove() method will remove a specific element from it instead of the clear() method that will clear the whole set. Let’s try an example:
# Create a Set
capitals = {"Berlin", "London"}
# Remove an Element
capitals.remove("Berlin")
# Print The Result
print(result)
It will show you only the “London” value since we’ve removed the “Berlin” value. The remove() method only accepts this parameter, so you add the element you need to remove, and if you specify more than one element, it will raise a TypeError error message.
6. Update() Method
This method is known as the update() method, will be helpful when you want to make changes to your set. It won’t change one element, but it will change the whole set by adding the second set to the first set. Let’s see how it works:
# Create Two Sets
capitals = {"Berlin", "London"}
capitals2 = {"Paris", "Moscow"}
# Update The Set
capitals.update(capitals2)
# Print The Result
print(capitals)
It will add the capitals2 set to the first one, the capital set. Any repeated value in the second set won’t be added again because you can’t have the same element twice in the set.
7. Symmetric_Difference_Update() Method
This symmetric_difference_update() method will try to find the symmetric difference in both sets, and it will update the set you are calling it. Meaning removing the items in both sets and inserting the items that are not presented in the two sets. Let’s see an example:
# Create Two Sets
capitals = {"Berlin", "London", "Paris"}
capitals2 = {"Paris", "Moscow", "Berlin"}
# Update The Set
capitals.symmetric_difference_update(capitals2)
# Print The Result
print(capitals)
It will see the elements not present in both sets and add them to the first one, the capital. Only the “London” & “Moscow” variables are not presented in both.
8. Pop() Method
This pop() method is fascinating. It will remove random elements from the sets. Sometimes it removes only one element and sometimes many elements. Let’s see an example:
# Create a Set
capitals = {"Berlin", "London", "Paris"}
# Remove Randomly Element From The Set
result = capitals.pop()
# Print The Result
print(result)
After running the code, it only shows the “Berlin” value in the set and removes all other values. It can be different in your case because it works randomly, not with a fixed equation.
9. Union() Method
The union() method will return a new one in a new variable that contains the distinct element from all the sets. But for, duplicate elements are excluded. Let me show you an example:
# Create Two Sets
capitals = {"Berlin", "London", "Paris"}
capitals2 = {"Moscow", "Paris", "Berlin"}
# Update The Set
result = capitals.union(capitals2)
# Print The Result
print(result)
The two values “Berlin” and “Paris” are mentioned in both sets, and it will add them to the newly created one, but it won’t add them twice since it is not accepted in sets to have a duplicate value.
Conclusion
Thanks for reading! Sets are beneficial in the python language since they remove duplicate elements from the list and tuples. They can be combined using mathematical operations and many other advantages. Many other sets methods are available, but we have already discussed the most used and useful ones.