Skip to main content

Command Palette

Search for a command to run...

Sets in Python

Sets are unordered collection of data items, which are also present in Python. We will learn about them today.

Updated
•3 min read•View as Markdown
Sets in Python
S
I build tools that make developers faster and help startups grow. I specialize in building agentic workflows, oss infrastructure, high-performance web systems, and creating technical content. I've spent years building products used by thousands, from developer-facing infrastructure at Keploy and Wizdom to building the user base for early-stage startups. My work sits at the intersection of agentic AI, open-source ecosystems, and growth engineering.

Introduction

Sets are unordered collection of data items. They store multiple items in a single variable. Set items are separated by commas and enclosed within curly brackets {}. Sets are unchangeable, meaning you cannot change items of the set once created. Sets do not contain duplicate items.

Example

info = {"Carla", 19, False, 5.9, 19}
print(info)

Output

{False, 19, 5.9, 'Carla'}

Here we see that the items of set occur in random order and hence they cannot be accessed using index numbers. Also sets do not allow duplicate values.

Accessing set items:

Using a For loop

You can access items of set using a for loop.

Example

info = {"Carla", 19, False, 5.9}
for item in info:
    print(item)

Output

False
Carla
19
5.9

Joining Sets

Sets in python more or less work in the same way as sets in mathematics. We can perform operations like union and intersection on the sets just like in mathematics.

I. Union and Update

The union() and update() methods prints all items that are present in the two sets. The union() method returns a new set whereas update() method adds item into the existing set from another set.

Example

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.union(cities2)
print(cities3)

Output

{'Tokyo', 'Madrid', 'Kabul', 'Seoul', 'Berlin', 'Delhi'}

Example

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.update(cities2)
print(cities)

Output

{'Berlin', 'Madrid', 'Tokyo', 'Delhi', 'Kabul', 'Seoul'}

II. intersection and intersection_update()

The intersection() and intersection_update() methods prints only items that are similar to both the sets. The intersection() method returns a new set whereas intersection_update() method updates into the existing set from another set.

Example

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.intersection(cities2)
print(cities3)

Output

{'Madrid', 'Tokyo'}

Example

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.intersection_update(cities2)
print(cities)

Output

{'Tokyo', 'Madrid'}

III. symmetric_difference and symmetric_difference_update()

The symmetric_difference() and symmetric_difference_update() methods prints only items that are not similar to both the sets. The symmetric_difference() method returns a new set whereas symmetric_difference_update() method updates into the existing set from another set.

Example

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.symmetric_difference(cities2)
print(cities3)

Output

{'Seoul', 'Kabul', 'Berlin', 'Delhi'}

Example

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.symmetric_difference_update(cities2)
print(cities)

Output

{'Kabul', 'Delhi', 'Berlin', 'Seoul'}

IV. difference() and difference_update()

The difference() and difference_update() methods prints only items that are only present in the original set and not in both the sets. The difference() method returns a new set whereas difference_update() method updates into the existing set from another set.

Example

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Seoul", "Kabul", "Delhi"}
cities3 = cities.difference(cities2)
print(cities3)

Output

{'Tokyo', 'Madrid', 'Berlin'}

Example

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Seoul", "Kabul", "Delhi"}
print(cities.difference(cities2))

Output

{'Tokyo', 'Berlin', 'Madrid'}

Conclusion

Thanks for reading this blog!! I hope you have learnt something new today and I wish you an amazing day ahead ❤

D

Neat! You covered most of the things about sets. But I think, you could also highlight the matter that sets do not contain duplicates with a suitable example. We have a built-in function 'set()', allows us to convert data structures(like lists & tuples) into sets. When solving problems we often face an issue about discarding duplicates. It would have been a lot better for beginners if you mentioned this concept.

However your all blogs are going amazing.Warm regards 😊👍.

3
S

Thanks a lot Debajyati, maybe I should have included those stuffs in this blog only... but I have a planned a different blog for that purpose, where I will discuss the difference between tuple, list, set and dictionary and how to inter-convert them. So, stay tuned for that !!

3
M

I learned sets from this blog. you are amazing

3
S

Thanks Mehebub, your appreciation means a lot to me.

3

Python : Advanced

Part 17 of 19

In this series, I will be discussing about some complicated/under-used features of Python, important libraries such as pandas; and also OOPs, Data Analytics and AI-ML.

Up next

Docstrings in python

Python docstrings are the string literals that appear right after the definition of a function, method, class, or module.