Why does the inner loop run 5000 times?

Screen Link:

I don’t understand the following sentence:

" Each item in the “outer” for loop spawns an “inner” for loop, which executes as many times as query has elements. This means that the loop will run len(query) * len(query) times, or 5000 * 5000 , which equals 25000000 "

Why would the inner loop run another 5000 times per item, this doesn’t sound logical?

Hi! I’ll try my best to explain this. A for loop is meant to iterate over a given sequence–sequentially. If you take a look at the screenshot, you can see that the for loop operates in a sequential manner. So, for each value in the outer sequence ([0,1,2]), it’ll execute the the inner loop ([0,1]).

NOTE: The outer loop print function + one of the inner loop print counts as one execution. Hence, in accordance to the lesson plan, you’ll get 6 total executions.

1 Like