Show what a Python set does with membership tests and with duplicate values.
A set is an unordered collection of unique, hashable items. The two things it is best at are membership tests and removing duplicates.
tags = ["python", "web", "python", "data", "web"]
unique = set(tags)
print(type(unique).__name__) # set
print(len(unique)) # 3 - duplicates are gone
print("python" in unique) # True
print("ruby" in unique) # False
# Building a set directly
numbers = {1, 2, 2, 3}
print(numbers) # {1, 2, 3}
print(len(numbers)) # 3The list of five tags becomes a set of three, because a set stores each item only once. Membership testing is where the set earns its keep: the in operator on a set is a hash lookup, so it is constant time, while the same test on a list has to scan element by element. On a list of a million items that is the difference between instant and visibly slow, and it is why converting to a set before a loop of membership tests is a standard optimisation.
Two behaviours to note. Sets are unordered, so there is no first element and the iteration order depends on the hash table — never rely on it. And equality is by content, so {1, 2} == {2, 1} is True, which is a different rule from lists, where order matters.