Sitemap

Python Data Structure: Set

3 min readNov 3, 2022

--

Hello, I am Şeyma, one of the participants of the SistersLab Women in Tech Academy project supported by Toplum Gonulluleri Vakfı (https://www.tog.org.tr/en/).

The project aims to empower people between the age of 20–28 who identify as women, through 3 months long software training and supporting training 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/

I will share with you what I learned about python as part of this beautiful project. I wish you good reading.

In this story, I will talk about sets. Now, I wish you good reading.

Press enter or click to view image in full size

Set Definition:

They cannot be accessed by index. They are not unordered. Which means that when you want to access a set, the order will be different each time and you will not be able to reach a clear value.

Sets cannot be changed, so they are immutable. Unlike tuples, we can add and remove new elements from the set. But we cannot change an element in the set.

{} is defined in parentheses. Elements are separated by commas.

It allows to movement of different types of data.

Get İclal Şeyma Koca’s stories in your inbox

Join Medium for free to get updates from this writer.

Two elements with the same value CANNOT be in place. They do not carry duplicate data.

The set allows using tuples in it. Because tuples, like sets, do not allow them to reach their indexes and do not allow any change. However, lists cannot be used within a set. Because it is a structure that allows accessing and changing list indexes.

Access to Elements in Set:

All elements can be accessed using the for a loop. But keep in mind that there is random order in the sets.

As a small note, it is possible to find the set length with len().

Adding Elements to a Set:

  • A new element is added to the set with add()
  • Multiple additions are made with update().

Deleting an Element from a Set:

Elements can be deleted from sets using the following methods.

  • remove()
  • discard()
  • pop()
  • clear()
  • del set

Operators that we can use on the set:

  • union() -> | : is used to combine two sets. “|” If you write the sign between the two sets you want to combine and equate it to a new set, you will have done this.
  • intersection() -> &: Takes only elements common in two sets. should be considered as an intersection set. By typing the & sign between the two sets you want to get the intersection sets, and equating it to another set, you perform the operation.
  • difference() -> -: Allows you to make a new set by taking the difference between two sets. Retrieves elements that are in the first set written but not in the second set written. If you put a “-” sign between the first set you want to get the difference of and equalize it to another set, that set will give the difference between the two sets.
  • symmetric_difference() -> ^: Allows you to get elements that are not common to both sets. You do this by placing the “^” sign between two sets and equating it to another set.

Copy Operations:

Copying in sets, which is the last subject of my article, is performed using the following methods.

  • copy()
  • =
  • set()

My story about the set is here. See you in the next story.

Press enter or click to view image in full size

--

--