Python: Data Structures 3— Set
Hello, I am one of the participants of SistersLab Women in Tech Academy project supported by the Community Volunteers Foundation (https://www.tog.org.tr/en/).
The project aims to empower people between the age of 20–28 who identify as woman, through 3 months long software training and supporting trainings to increase their participation in the workforce in the IT sector.
You can access detailed information about the project using this link: https://sisterslab.co/women-in-tech-academy/
In this publication, I will focus on what we had recently talked about; so today’s topic will be: sets.
Sets:
- are unordered,
- are immutable; their elements cannot be changed. However, even though it is not possible for us to change an element in a set, we can add or delete an item from a set. This is a unique property, which distinguishes sets from the tuples,
- allow more than one type of data to be stored,
- do not allow duplicate elements.
- can be shown with {}, but here, it is important not to confuse sets with dictionaries as they have a similar symbol. Also, the items in a set should be separated using a comma.
As mentioned above, besides different data types, data with different structures can also be stored in a set.
We already know that, when we want to create an empty tuple or an empty list, we basically type:
- my_list = []
- my_tuple = ()
So, we would guess, “well then, we create an empty set by typing: {}
But that is not the case in terms of a set.
Get Melisa B.’s stories in your inbox
Join Medium for free to get updates from this writer.
To create an empty set,
Accessing to Items in a Set
In order to see the items in a list, we can use the for loop.
In order to check if an item is present in a set, we can use “in”. Such as:
Adding an Item to the Set
- add()
- update() can be used to add an item to the set.
However, if an item already exists in a set, then none of these methods can be used to add the same item to the set. We already know that sets do not allow for items to be duplicated, thus it makes sense. :)
Removing an Item from a Set
- remove()
- discard()
- pop()
- clear()
are the functions and commands that can be used to remove an item from a set.
Besides, we can use “del” + [name of the set] to delete the whole set.
Operators
- union() -> |
- intersection() -> &
- difference() -> -
- symmetric_difference() -> ^
We can use these to unite the sets, find the intersecting elements in a set, find the difference of elements between sets and find the symmetric difference between sets. The symmetric difference is the group of elements that exist in one of the sets, but not in both.
Here are some examples and their outputs:
Copying a Set
- copy()
- =
- set()
Here, if we run these lines we see that, since we copied the set in names1 into names2, when we add a new item in the first set, the item is added automatically to the second set as well.
Subset & Superset
Here using issuperset and issubset we can find out: if a set has all the items the other set has(superset) along with some other items that the other set does not have, and if a set has some of the items that the other set has(subset).
We can also use isdisjoint to see if two sets are separate.
If we want to create an immutable set, we can use frozenset.
This code produces an error since it is not possible to add an item from a frozen set.
These are the main points about sets and this concludes our data structures list. :)




