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)) # 3Membership testing with in is very fast on a set:
print("a" in unique) # True
print("z" in unique) # FalseSets 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.
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.
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.