Syllabus Lesson 33 of 239 · Data Structures
Data Structures

Sets: Uniqueness and Membership

A set is an unordered collection of unique items. Duplicates simply vanish:

nums = {1, 2, 2, 3, 3, 3}
print(nums)   # {1, 2, 3}

Passing a list to set() is the quickest way to remove duplicates:

names = ["a", "b", "a", "c", "b"]
unique = set(names)
print(len(unique))   # 3

Membership testing with in is very fast on a set:

print("a" in unique)   # True
print("z" in unique)   # False

Sets support math-style operations. Use | for union (everything in either) and & for intersection (only what is in both):

a = {1, 2, 3}
b = {2, 3, 4}
print(a | b)   # {1, 2, 3, 4}
print(a & b)   # {2, 3}

Sets have no index, so you cannot write myset[0]. Reach for a set when you care about uniqueness or fast lookups, not order.

Your turn

Given tags = ["py", "web", "py", "data", "web"], build a set unique_tags from it. Then, with a = {1, 2, 3} and b = {3, 4, 5}, compute their intersection into common.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output