DEV Community

Cover image for Sets in Python 🐍
Christina Blakely
Christina Blakely

Posted on

Sets in Python 🐍

What are sets and why should you care about them?

Python sets are collections of data that remove duplicates. This is mainly done for the purposes of identifying membership, as well as comparing and contrasting the differences between sets. Utilizing the built-in set data type is more efficient than looping over and manipulating lists, arrays, or dictionaries.

Sets are mutable, meaning that we can add and remove elements from it. The elements within the set are immutable, hashable objects. This is why sets are so efficient; the set elements are converted to its hash value, which allows for less expensive comparison between elements. Using the frozenset() built-in function creates an immutable version of a set object. I won’t be covering it in this post, but you can read more on it here.

Let’s see how to get started with sets and how to use them.

You can create a set with either curly braces or using the set() function on a list, but remember that a pair of empty curly braces creates a dictionary.

set_a = {red, pink, yellow} 
set_a = set([red, pink, yellow])
Enter fullscreen mode Exit fullscreen mode

There are several methods that can be used on sets.

Let's practice with these two sets:

set_a = {'red','pink’, ‘yellow', 'orange'}
set_b = {'blue’, ‘green’, ‘yellow', 'orange'}
Enter fullscreen mode Exit fullscreen mode

.remove() removes a specified element from a set. If you attempt to remove an element that is not a member of a set, it raises a KeyError.

set_a.remove('yellow') 
print(set_a)
# set_a = {'pink', 'orange', 'red'}
set_a.remove('purple')
print(set_a)
# KeyError: 'purple'
Enter fullscreen mode Exit fullscreen mode

.discard() removes a specified element from a set, similar to the .remove() method. The difference with this method is that if it tries to discard an element that is not present in the set, it does not return an error.

set_a.discard('red') 
print(set_a)
# {'yellow', 'pink', 'orange'}
set_a.discard('purple')
print(set_a)
# {'yellow', 'pink', 'red', 'orange'}
Enter fullscreen mode Exit fullscreen mode

.add() adds a new element to the set.

set_a.add('purple')
print(set_a)
# {‘red’, ‘orange’, ‘purple’, ‘yellow’, ‘pink’}
Enter fullscreen mode Exit fullscreen mode

.pop() removes a random element from the set. It takes no arguments.

print(set_a.pop())
# red
Enter fullscreen mode Exit fullscreen mode

.union() returns all the elements in all the sets being used. It does not update the main set.

print(set_a.union(set_b))
# {'red', 'pink', 'orange', 'yellow', 'blue’, ‘green’, ‘yellow'}
print(set_a)
# {'red', 'yellow', 'pink'}
Enter fullscreen mode Exit fullscreen mode

.copy() allows you to make a copy of a set. There are two ways to do this, depending on if you want to update changes from original set to the copy or keep the original set in-tact.

new_set = set_a
# If you do it this way, changes made to set_a will show up in new_set
new_set = set_a.copy()
# new_set will remain however set_a was when it was copied.
Enter fullscreen mode Exit fullscreen mode

.clear() is pretty self-explanatory. This method removes all elements from a set.

set_a.clear()
print(set_a)
# set()
Enter fullscreen mode Exit fullscreen mode

.intersection() will return the common elements from the sets in use. The shorthand for this method is &.

print(set_a.intersection(set_b))
print(set_a & set_b)
# {'yellow', 'orange'}
Enter fullscreen mode Exit fullscreen mode

.symmetric_difference() will return the elements present in either of the sets but not in both. It is known as the union of the sets without the intersection. The shorthand for this method is ^.

print(set_a.symmetric_difference(set_b))
print(set_a ^ set_b)
# {'pink', 'blue’, ‘green', 'red'}
Enter fullscreen mode Exit fullscreen mode

.isdisjoint() returns True if two sets are disjoint sets. Two sets are disjoint when there are no shared elements.

print(set_a.isdisjoint(set_b))
# False
Enter fullscreen mode Exit fullscreen mode

.issubset() returns True if all elements of one set are present in another set.

set_c = {1, 2, 3}
set_d = {1, 2, 3, 4, 5, 6, 7}
print(set_c.issubset(set_d))
# True
print(set_d.issubset(set_c))
# False
Enter fullscreen mode Exit fullscreen mode

Sets are not that complex at all, but it can make your code cleaner and your life easier. Feel free to comment or reach out. ✨

Top comments (0)