Exploring the Power of Sets in Python: A Comprehensive Guide

Learn How Sets Can Help You Streamline Your Code and Improve Performance

Vicente
3 min readApr 28, 2023

A set in python is a collection of unique and unordered items. Mathematical set operations like union, intersection, and difference can be carried out on sets. Set helps us to remove duplicates and filtering out undesirable data form large datasets. In this post, we will examine the fundamentals of sets in Python as well as their most typical methods and use cases.

To create a set in Python, we use curly brackets {} or the built-in set() function. Here is a diagram how to define a set.

Let’s see some methods to manipulate sets in python.

add(item): Adds an element to a set. If the element already exists in the set, it will not be added again.

social_media = {"Facebook", "Tiktok", "Instagram"}
social_media.add("Linkedin")
print(social_media) # foutput=====> {"Facebook", "Tiktok", "Instagram", "Linkedin"}

remove(item): Removes an element from a set. If the element does not exist in the set, it will raise a KeyError.

social_media = {"Facebook", "Tiktok", "Instagram","Linkedin"}
social_media.remove("Linkedin")
print(social_media) # output ====> {"Facebook", "Tiktok", "Instagram"}

intersection(set): Returns a new set with elements that exist in both sets.

social_media = {"Facebook", "Tiktok"}
more_social_media = {"Instagram", "Linkedin","Facebook"}
all_social_media = social_media.intersection(more_social_media)
print(all_social_media) # output ====> {"Facebook"}

union(set): Returns a set resulting from combining A and B (duplicate data are removed)

social_media = {"Tiktok","Linkedin","Facebook"}
more_social_media = {"Instagram", "Linkedin","Facebook"}
my_union = social_media.union(more_social_media)
print(my_union) # output ====> {"Tiktok","Linkedin","Facebook","Instagram"}

difference(set): Returns a new set with elements that exist in the first set but not in the second set.

social_media = {"Facebook", "Tiktok","Youtube"}
social_video = {"Titok", "Youtube"}
unique_social_media = social_media.difference(social_video)
print(unique_social_media) # output ====> {"Facebook"}

In shorts sets are a powerful data structure that can help us remove duplicates, filter data, and perform mathematical set operations. We can define a set using brackets or the set() function, and also we can manipulate them with set methods. Always consider that sets are unordered and do not allow duplicates. When working with large datasets, using sets can improve performance and streamline your code.

Thanks for reading this article. I hope you learn something new! 😊👨🏽‍💻

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Vicente
Vicente

Written by Vicente

Hi,I'm a software developer with a passion for front-end development. I'm also a talented designer.

No responses yet

Write a response