# GP: Gender Gap In College Degrees. Nested Loop

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.

1. Create a list of lists
``````cats = [stem_cats, lib_arts_cats, other_cats]
``````
1. Outer and inner loops
``````for outer_counter, outer_list_name in enumerate(cats):
for inner_counter, inner_list_value in enumerate(cats[outer_counter]):
``````
1. 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.

1. Followed by the rest of subplot editing code.

Just wanted to share the use of enumerate().

Last mission screen here

Full solution on github

2 Likes

Hello @semenchuk.ivan
Neat code, noticed the comments are explanatory. I would suggest you add a conclusion.
Happy coding

That’s a very smart trick. I will definitely read more about enumerate(), thanks for sharing.

Thank you for sharing this and teaching me something new!
This `enumerate() ` function is so wonderful. It gives your counter and values of each iteration! This can be very useful in many occasions.

You have used enumerate function very cleverly in this project. Thanks again for sharing this awesome trick!