This post intends to teach you everything about Python’s set. Honestly, I hope you can learn here how useful this statement can be, I think sets are the most underrated data structure in python. The post targets in showing you how to use the main set operations: add, remove, union, intersection, set to list, etc
1. Defining a Set
In python, a set is a collection of unique values that are unordered and unindexed which are defined by curly brackets.
>>> my_set = {"San Diego", "New York", "Santa Barbara", "Miami"}
>>> my_set
>>> {'San Diego', 'New York', 'Miami', 'Santa Barbara'}
Clearly, as you can see above, the set came out unordered. Thus, a list structure should be used if the order of the elements matters to you.
Also, Sets store unique values, if we define a set with a repeated value, then only one representation of the value will be stored.
Despite being defined twice in the set, the city “New York” only shows once.
>>> my_set = {"San Diego", "New York", "Santa Barbara", "Miami", "New York"}
>>> my_set
>>> {'San Diego', 'Miami', 'New York', 'Santa Barbara'}
Moreover, you can remove repeated items from a list by converting it to a set and then back to s list.
>>> my_list = [1, 1, 1, 1, 1, 2, 2, 3, 3, 1, 3, 3]
>>> new_list = list(set(my_list))
>>> new_list
>>> [1, 2, 3]
An application for this is on removing repeated DNA sequences for a large file.
2. Add an Item to a Set
Adding an element to a set is simple can be accomplished by calling add() in a set that was already initiated.
>>> my_set = {"San Diego", "New York", "Santa Barbara", "Miami"}
>>> # add the string "Dallas" to set
>>> my_set.add("Dallas")
>>> my_set
>>> # as you can see, "Dallas" was added to set
>>> {'San Diego', 'Dallas', 'New York', 'Miami', 'Santa Barbara'}
3. Remove an Item to a Set
An item is removed from a set by entering the target item to the method remove() or discard().
>>> my_set = {"San Diego", "New York", "Santa Barbara", "Miami"}
>>> my_set.remove("Miami")
>>> my_set
>>> # remove "Miami" from set
>>> {'San Diego', 'New York', 'Santa Barbara'}
4. Merging – Union Two sets
Sets are merged (union) by calling the method union() which requires another set as a parameter.
>>> my_set = {"San Diego", "New York"}
>>> my_set_2 = {"Santa Barbara", "Miami"}
>>> my_set_3 = my_set.union(my_set_2)
>>> my_set_3
>>> {'San Diego', 'Miami', 'New York', 'Santa Barbara'}
In conclusion, I hope you see why a set is the most underrated data structure. Furthermore, I hope this tutorial taught you some lessons on how to use the set statement on Python. Please comment below if there is anything else you would love to learn.
5. Set intersection
You can find the set intersection with another set(s) by using the method intersection() which requires another set as a parameter.
>>> my_set = {"San Diego", "New York"}
>>> my_set_2 = {"San Diego", "Santa Barbara", "Miami"}
>>> my_intersection = my_set.intersection(my_set_2)
>>> my_intersection
>>> {"San Diego"}
If you have more than one set, you can do it as the following:
>>> my_set = {"San Diego", "New York"}
>>> my_set_2 = {"San Diego", "Santa Barbara", "Miami"}
>>> my_set_3 = {"San Diego", "Miami"}
>>> my_intersection = my_set.intersection(my_set_2, my_set_3)
>>> my_intersection
>>> {"San Diego"}
6. Set to list
It is simple to convert a set to a list – just call the function list() and pass a set.
>>> my_set = {"San Diego", "New York"}
>>> my_list_from_set = list(my_set)
>>> my_list_from_set
>>> ["San Diego", "New York"]
>>> type(my_list_from_set)
>>> <class 'list'>
More Resources
Here are three of my favorite Python Books in case you want to learn more about it.
- Python Cookbook, Third Edition by David Beazley and Brian K. Jones
- Learning Python, 5th Edition Fifth Edition by Mark Lutz
- Python Pocket Reference: Python In Your Pocket by Mark Lutz