Search⌘ K
AI Features

Introduction to Sets

Explore the fundamentals of Python sets, including what they are, how they store data without duplicates, and the importance of immutability for set elements. Understand how to access and iterate over sets, and discover practical uses like membership testing and eliminating duplicates in your programs.

What are sets?

Sets are collections of data items that do not contain duplicate entries.

Python 3.8
a = set( ) # empty set, use ( ) instead of { }
b = {20} # set with one item
c = {'Thomas', 25, 34555.50} # set with multiple items
d = {10, 10, 10, 10} # only one 10 gets stored
print(a)
print(b)
print(c)
print(d)

While storing an element in a set, its hash value is computed using a hashing technique to determine where it should be stored in the set.

Since the hash value of an element will always be the same, no matter in which order we insert the ...