Python Sets Methods: A Practical Guide with Examples

wordpediax
4 min readNov 5, 2023
Photo by Jared Berg on Unsplash

Sets are a fundamental data structure in Python, and they come equipped with various methods that make working with them a breeze.

In this guide, we will explore Python set methods in detail and provide practical examples to help you understand how to use them effectively.

1. add()

The add() method allows you to add an element to a set. If the element is already present, it won't be added again.

Example:

fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
# Resulting fruits: {"apple", "banana", "cherry", "orange"}

In this example, the add() method is used to add the element "orange" to the fruits set.

2. remove()

The remove() method is used to remove a specific element from a set. If the element does not exist, it will raise an error.

Example:

fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
# Resulting fruits: {"apple", "cherry"}

3. discard()

The discard() a method is similar to remove(), but it won't raise an error if the element is not found.

Example:

fruits = {"apple", "banana", "cherry"}
fruits.discard("orange")
# Resulting fruits: {"apple", "banana", "cherry"}

In this example, the discard() method is used to attempt the removal of the element "orange," which is not present in the set.

You Might Like

4. clear()

The clear() method removes all elements from the set, making it empty.

Example:

fruits = {"apple", "banana", "cherry"}
fruits.clear()
# Resulting fruits: set()

In this example, the clear() method is used to clear all elements from the fruits set, leaving it empty.

5. update()

The update() method allows you to add elements from another set (or any iterable) to the current set.

Example:

fruits = {"apple", "banana", "cherry"}
new_fruits = {"orange", "kiwi"}
fruits.update(new_fruits)
# Resulting fruits: {"apple", "banana", "cherry", "orange", "kiwi"}

In this example, the update() method is used to add elements from the new_fruits set to the fruits set.

6. union()

The union() method returns a new set that contains all the elements from both the calling set and another set.

Example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
# Resulting union_set: {1, 2, 3, 4, 5}

In this example, the union() method is used to create a new set, union_set, containing all the elements from both set1 and set2.

7. intersection()

The intersection() method returns a new set containing the common elements between the calling set and another set.

Example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
# Resulting intersection_set: {3}

In this example, the intersection() method is used to create a new set, intersection_set, containing the common element 3 between set1 and set2.

You Might Like

8. difference()

The difference() method returns a new set containing the elements that exist in the calling set but not in another set.

Example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
# Resulting difference_set: {1, 2}

In this example, the difference() method is used to create a new set, difference_set, containing elements that are in set1 but not in set2.

9. symmetric_difference()

The symmetric_difference() method returns a new set containing elements that exist in either of the sets but not in both.

Example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1.symmetric_difference(set2)
# Resulting symmetric_difference_set: {1, 2, 4, 5}

In this example, the symmetric_difference() method is used to create a new set, symmetric_difference_set, containing elements that are in either set1 or set2 but not in both.

10. issubset() and issuperset()

The issubset() and issuperset() methods allow you to check if a set is a subset or a superset of another set, respectively.

Example:

set1 = {1, 2, 3}
set2 = {1, 2}
set3 = {1, 2, 3, 4}
is_subset = set2.issubset(set1) # True
is_superset = set3.issuperset(set1) # True

In this example, is_subset is True because set2 is a subset of set1, and is_superset is True because set3 is a superset of set1.

Conclusion

Python sets, with their collection of methods, provide a powerful and efficient way to work with unique elements and perform set operations.

By mastering these methods and understanding their applications, you can become a more proficient Python programmer, capable of handling a wide range of tasks efficiently.

Whether you’re managing user permissions, finding unique elements in data, or synchronizing data between sources, sets and their methods are valuable tools in your programming toolbox.

Looking for more exciting articles?

https://codingstreets.com/blog/

--

--

wordpediax

I like to read and write articles on tech topics like Python, ML, AI. Beside this, I like to thread life into words and connect myself with nature.