Show what a Python set does with membership tests and with duplicate values.
A set is a collection that keeps every item only once, and it does not keep the items in any particular order. A set is good at two things: checking whether an item is inside it, and removing duplicates from a list.
tags = ['python', 'web', 'python', 'data', 'web']
unique = set(tags)
print(type(unique).__name__)
print(len(unique))
print('python' in unique)
print('ruby' in unique)
# You can also build a set directly.
numbers = {1, 2, 2, 3}
print(numbers)
print(len(numbers))
The list of five tags becomes a set of three, because a set stores each item only one time. The membership test is where a set earns its keep. The in operator on a set looks the answer up directly, so it takes the same short time no matter how big the set is. The same test on a list has to walk through the list one item at a time. On a list with a million items that is the difference between an instant answer and a slow one, which is why it is common to turn a list into a set before you do a lot of membership tests.
Two behaviours are worth remembering. A set has no order, so there is no first item and the order you get when you walk through it depends on the internal table, so never rely on it. And two sets are equal when they hold the same items, so {1, 2} == {2, 1} is True. That is a different rule from lists, where the order always matters.