In Python, range(num)
(more or less) returns a list of numbers from 0 through num - 1
. It follows that range(len(my_list))
will generate a list of numbers from 0 through the length of my_list
minus one. This is frequently useful, because the generated numbers are the indices of each item in my_list
(Python lists start counting at 0). For example, range(len(["a", "b", "c"]))
is [0, 1, 2]
, the indices needed to access each item in the original list. ["a", "b", "c"][0]
is "a"
, and so on.
In Python, the for x in mylist
loop iterates through each item in mylist
, setting x
to the value of each item in order. One common pattern for Python for loops is the for x in range(len(my_list))
. This is useful, because you loop through the indices of each list item instead of the values themselves. It's almost as easy to access the values (just use my_list[x]
) but it's much easier to do things like access the preceding value (just use my_list[x-1]
, much simpler than it would be if you didn't have the index!).
In your example, idx
is tracking the index of each list item as the program iterates through search_list
. In order to retrieve values from search_list
, the program uses search_list[idx]
, much like I used my_list[x]
in my example. The code then assigns maximum_score_index
to the index itself, a number like 0
, 1
, or 2
, rather than the value. It's still easy to find out what the maximum score is, with search_list[maximum_score_index]
. The reason idx
is not being used as a list accessor in the second case is because the program is storing the index itself, not the value of the array at that index.