DEV Community

Lourenço Costa
Lourenço Costa

Posted on

Python course: Sets

This is another data type used to store values as a collection in Python. Sets offer a very interesting feature, though: uniqueness of elements. This means that if you add the same element twice (or more) to a set, it will be ignored! This is useful if you want to ensure your collection does not contain any duplicated elements:

group_1 = {"jim", "michael", "michael", "jan"} # Notice that "michael" was added twice

group_1.add("pam") # This is how you can add new elements

group_2 = set(["jim", "ryan", "kelly", "ryan"]) # It can be created this way too. Also, notice that "ryan" was added twice

print(group_1) # => {'pam', 'jan', 'jim', 'michael'}
print(group_2) # => {'ryan', 'kelly', 'jim'}
Enter fullscreen mode Exit fullscreen mode

Notice that group_1 and group_2 don't contain any repeated elements, even though I tried to add duplicates into them.

Also, notice that when I print() them, the order of the elements is not respected! This has to do with some internal specifications on how Python stores these values in memory (it uses hash tables). As a consequence of that, you cannot access individual elements by index in a set, as we do in lists and tuples! As a workaround to this limitation, you can easily create a list out of a set:

group_1 = list({"jim", "michael", "michael", "jan", "pam"}) # A list created out of a set. the second "michael" will be ignored

print(group_1) # => ['jan', 'jim', 'pam', 'michael']
Enter fullscreen mode Exit fullscreen mode

This procedure of converting a type into another (in this case, a set into a list), is known as “casting”.

Apart from this validation for uniqueness, another interesting use case for sets is performing union and intersection operations of elements in different sets.


group_1 = {"jim", "michael", "michael", "jan", "pam"}
group_2 = {"jim", "ryan", "kelly", "ryan"}

group_difference = group_1.difference(group_2)
print(group_difference) # => {'michael', 'pam', 'jan'} 

group_union = group_1.union(group_2)
print(group_union) # => {'jim', 'pam', 'jan', 'ryan', 'kelly', 'michael'}

easier_union = group_1 | group_2 # The same as the one above 
print(easier_union) # => {'jim', 'pam', 'jan', 'ryan', 'kelly', 'michael'}

group_intersection = group_1.intersection(group_2)
print(group_intersection) # => {'jim'} 

easier_intersection = group_1 & group_2 # The same as the one above 
print(easier_intersection) # => {'jim'}
Enter fullscreen mode Exit fullscreen mode

If the symbols "|" and "&" are unfamiliar to you (as seen in easier_union and easier_intersection), check out the bitwise operators in the Operators post.


😊 Enjoying this series? The full book contains even more content! Support my work by purchasing the complete book in digital or paperback formats. Click below to find out more.
Alt text


Be aware that you are eligible get a Wise card or your first international transfer, up to 500 EUR, free! I've been using their service for years and it's a great way to send/receive money abroad, creating disposable virtual cards, and more. Get it here: Sponsored link.

By now you may have heard of ElevenLabs. Their AI voice cloning service is simply off the charts! Check them out: Sponsored link.

Follow me around:
LinkedIn Buy me a coffee GitHub Dev.to

Top comments (1)

Collapse
 
ali_arefi_065bad0012ad24b profile image
Ali Arefi

are you programmer can you tech me please