Chapter 5: Sets and dictionaries

When are sets useful?

face Josiah Wang

You cannot access an element in a set. Why? Hint: Look at the second half of my definition of a set (“A set does not have duplicate elements, nor should the ordering be important.”)

>>> numbers = {1, 2, 3}
>>> print(numbers[1])
???

You will need to convert a set to a list if you need to access individual elements.

>>> number_set = {1, 2, 3}
>>> number_list = list(number_set)
>>> print(number_list[1])
2

When are sets useful?

So why do you need sets when you cannot even access its elements? Why not just use a list?

The power of sets comes when you need the elements to be unique.

For example, it is really fast to check whether an element is in a set.

The following piece of code compares the speed of membership checks using lists and using sets. If you run the code, you will find that set is much much faster. Run the code multiple times to ensure that the pattern is consistent. If you run out of memory, reduce the number in line 3 to something smaller.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import time

numbers = range(1000000)

number_list = list(numbers)
start_time = time.time()
print(51232422 in number_list)
end_time = time.time()
print(f"list took {end_time - start_time} seconds")

number_set = set(numbers)
start_time = time.time()
print(51232422 in number_set)
end_time = time.time()
print(f"set took {end_time - start_time} seconds")

In a single run on my computer, list took 0.00965 seconds and set took 4.935e-05 seconds (or 0.000049 seconds).

set is fast because the elements are stored and retrieved via hashing.

So always use sets for membership checks with the in operator when you can!