Lesson 12
Sets & Uniqueness
A set stores unique hashable values. Repeated values collapse into one item. Sets do not promise a meaningful display order.
Use in for membership and & for intersection: values present in both sets. The | operator makes a union.
Use sorted when you need a predictable list for display. An empty set is set(); {} creates an empty dictionary.
tags = {"python", "web", "python"}
print(len(tags))
print("web" in tags)Check your understanding
Which creates an empty set?
CHALLENGE
Use a set to remove duplicates from [3, 1, 3, 2]. Print the unique values as a sorted list.
Practice →