Python Set Methods

The set in Python is an unordered collection of unique items. Sets are mutable and provide several methods to manipulate and perform operations like union, intersection, and difference. In this post, we will explore the complete list of Python set methods using the set {"AviosIT", 2030, "Python", "Programming", "Computer"}.

Python Set Methods
Python Set Methods

1. The .add() Method

Adds a single element to the set.

data = {"AviosIT", 2030, "Python", "Programming", "Computer"}
data.add("Developer")
print(data)
{"AviosIT", 2030, "Python", "Programming", "Computer", "Developer"}

2. The .update() Method

Adds multiple elements to the set from an iterable.

data = {"AviosIT", 2030, "Python", "Programming", "Computer"}
data.update(["AI", "Machine Learning"])
print(data)
{"AviosIT", 2030, "Python", "Programming", "Computer", "AI", "Machine Learning"}

3. The .remove() Method

Removes a specified element. Raises a KeyError if the element does not exist.

data = {"AviosIT", 2030, "Python", "Programming", "Computer"}
data.remove("Python")
print(data)
{"AviosIT", 2030, "Programming", "Computer"}

4. The .discard() Method

Removes a specified element without raising an error if the element does not exist.

data = {"AviosIT", 2030, "Python", "Programming", "Computer"}
data.discard("Java")  # No error if "Java" does not exist
print(data)
{"AviosIT", 2030, "Python", "Programming", "Computer"}

5. The .pop() Method

Removes and returns an arbitrary element from the set.

data = {"AviosIT", 2030, "Python", "Programming", "Computer"}
removed_item = data.pop()
print(data)
{"AviosIT", 2030, "Programming", "Computer"}

6. The .clear() Method

Removes all elements from the set.

data = {"AviosIT", 2030, "Python", "Programming", "Computer"}
data.clear()
print(data)
set()

7. The .union() Method

Returns a new set that contains all unique elements from two or more sets.

set1 = {"AviosIT", 2030, "Python"}
set2 = {"Programming", "Computer", "AI"}
result = set1.union(set2)
print(result)
{"AviosIT", 2030, "Python", "Programming", "Computer", "AI"}

8. The .intersection() Method

Returns a new set containing elements common to both sets.

set1 = {"AviosIT", 2030, "Python"}
set2 = {2030, "Programming", "Python"}
result = set1.intersection(set2)
print(result)
{2030, "Python"}

9. The .difference() Method

Returns a new set with elements in the first set that are not in the second set.

set1 = {"AviosIT", 2030, "Python"}
set2 = {2030, "Programming", "Python"}
result = set1.difference(set2)
print(result)
{"AviosIT"}

10. The .symmetric_difference() Method

Returns a new set with elements in either of the sets but not in both.

set1 = {"AviosIT", 2030, "Python"}
set2 = {2030, "Programming", "Python"}
result = set1.symmetric_difference(set2)
print(result)
{"AviosIT", "Programming"}

Conclusion

We have covered the most common and advanced set methods in Python using the provided set {"AviosIT", 2030, "Python", "Programming", "Computer"}. Master these methods to enhance your Python programming skills. Happy coding!

advertise
advertise
advertise
advertise