For this guided project I used python’s built-in enumerate() function which allowed me to use one nested loop instead of multiple loops. I am seeing some other solutions with a nested loop as well.
The advantage of enumerate() is that we don’t have to specify how many elements we have in our lists, 5, 6, or any number, enumerate() counts and loops through them all.
- Create a list of lists
cats = [stem_cats, lib_arts_cats, other_cats]
- Outer and inner loops
for outer_counter, outer_list_name in enumerate(cats):
for inner_counter, inner_list_value in enumerate(cats[outer_counter]):
- Followed by:
ax = fig.add_subplot(6, 3, outer_counter + (inner_counter * 3) + 1)
outer_counter + (inner_counter * 3) + 1 generates a sequence as follows: 1, 4, 7, 10, 13, 16, 2, 5, 8, 11, 14, 3, 6, 9, 12, 15, 18, these are the positions of the 17 subplots. I don’t have to explicitly code that I don’t want a plot in position 17.
- Followed by the rest of subplot editing code.
Just wanted to share the use of enumerate().
Last mission screen here
Full solution on github