Sets in Python

Jayashree domala
2 min readSep 15, 2020

--

A guide to knowing more about set data type in python!

Sets (Source)

Sets are unordered collections of unique elements. This means that there can only be one representative of the same object.

Sets

>>> set1 = set()
>>> set1
set()

Add values in the set

>>> set1.add(11)
>>> set1
{11}

It might look like a dictionary because of the curly braces but it’s not because there are no key-value pairs.

>>> set1.add(22)
>>> set1
{11, 22}
>>> set1.add(22)
>>> set1
{11, 22}

Even after adding the number ‘22’ again, it cannot be seen in the set because sets do not accept repeated objects and accept only unique values.

Typecasting

When we have a list with repeated values, it can be typecast to a set to get only the unique ones.

>>> list1 = [11,11,22,33,44,44,44,44,55]
>>> set(list1)
{11, 22, 33, 44, 55}

Refer to the notebook here.

Beginner-level books to refer to learn Python:

Advance-level books to refer to learn Python:

Reach out to me: LinkedIn

Check out my other work: GitHub

--

--

Jayashree domala
Jayashree domala

Written by Jayashree domala

Self-driven woman who wishes to deliver creative and engaging ideas and solutions in the field of technology.

No responses yet