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…
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.
[embed]3 different examples; integer, string, and mix sets. mix_set is written on the console using print() function.
As mentioned above, besides different data types, data with different structures can also be stored in a set.
[embed]my_set has a tuple in the set whereas my_set2 has a list.
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.
To create an empty set,
[embed]We can check if the item we created is actually a set by using the type function on line 3.
Accessing to Items in a Set
In order to see the items in a list, we can use the for loop.
[embed]This for loop prints the items in the set “food” on the screen.
In order to check if an item is present in a set, we can use “in”. Such as:
[embed]
Adding an Item to the Set
- add()
- update() can be used to add an item to the set.
[embed]
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. :)
[embed]In this example, the new item “two” would not be added to the set, since “two” already exists in the set.
[embed]In this example, we used the update function. Here, “six” would be added to the set while “three” would not be added again since three is already in the set.
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.
[embed]
Besides, we can use “del” + [name of the set] to delete the whole set.
[embed]Here we see how a set can be deleted.
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:
[embed]
Copying a Set
- copy()
- =
- set()
[embed]
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
[embed]
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.
[embed]
If we want to create an immutable set, we can use frozenset.
[embed]
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. :)

메타데이터
- post_id
- b05a77aca697
- slug
- data-structures-3-set-b05a77aca697
- url
- https://medium.com/@dmelisab/data-structures-3-set-b05a77aca697
- canonical_url
- https://medium.com/@dmelisab/data-structures-3-set-b05a77aca697
- author_url
- https://medium.com/@dmelisab
- status
- ok
- fetched_at
- 2026-07-26 13:28:39