Processing Lists Using Indices

The loops over lists that we have written so far have been used to access list items. But what if we want to change the items in a list? For example, suppose we want to double all of the values in a list. The following doesn’t work:

 >>>​​ ​​values​​ ​​=​​ ​​[4,​​ ​​10,​​ ​​3,​​ ​​8,​​ ​​-6]
 >>>​​ ​​for​​ ​​num​​ ​​in​​ ​​values:
 ...​​ ​​num​​ ​​=​​ ​​num​​ ​​*​​ ​​2
 ...
 >>>​​ ​​values
 [4, 10, 3, 8, -6]

Each loop iteration assigned an item in the list values to variable num. Doubling that value inside the loop changes what num refers to, but it doesn’t mutate the list object. For example, after one iteration of the loop, the list is unchanged and num refers to 8 (twice its original value):

images/loop/iterator_overwrite.png

Let’s add a call on function print to show how the value that num refers to changes during each iteration:

 >>>​​ ​​values​​ ​​=​​ ​​[4,​​ ​​10,​​ ​​3,​​ ​​8,​​ ​​-6]
 >>>​​ ​​for​​ ​​num​​ ​​in​​ ​​values:
 ...​​ ​​num​​ ​​=​​ ​​num​​ ​​*​​ ​​2
 ...​​ ​​print(num)
 ...
 8
 20
 6
 16
 -12
 >>>​​ ​​print(values)
 [4, 10, 3, 8, -6]

The correct approach is to loop over the indices of the list. If variable values refers to a list, then len(values) is the number of items it contains, and the expression range(len(values)) produces a sequence containing exactly the indices for values:

 >>>​​ ​​values​​ ​​=​​ ​​[4,​​ ​​10,​​ ​​3,​​ ​​8,​​ ​​-6]
 >>>​​ ​​len(values)
 5
 >>>​​ ​​list(range(5))
 [0, 1, 2, 3, 4]
 >>>​​ ​​list(range(len(values)))
 [0, 1, 2, 3, 4]

The list that values refers to has five items, so its indices are 0, 1, 2, 3, and 4. Rather than looping over values, you can iterate over its indices, which are produced by range(len(values)):

 >>>​​ ​​values​​ ​​=​​ ​​[4,​​ ​​10,​​ ​​3,​​ ​​8,​​ ​​-6]
 >>>​​ ​​for​​ ​​i​​ ​​in​​ ​​range(len(values)):
 ...​​ ​​print(i)
 ...
 0
 1
 2
 3
 4

Notice that we called the variable i, which stands for index. You can use each index to access the items in the list:

 >>>​​ ​​values​​ ​​=​​ ​​[4,​​ ​​10,​​ ​​3,​​ ​​8,​​ ​​-6]
 >>>​​ ​​for​​ ​​i​​ ​​in​​ ​​range(len(values)):
 ...​​ ​​print(i,​​ ​​values[i])
 ...
 0 4
 1 10
 2 3
 3 8
 4 -6

You can also use them to modify list items:

 >>>​​ ​​values​​ ​​=​​ ​​[4,​​ ​​10,​​ ​​3,​​ ​​8,​​ ​​-6]
 >>>​​ ​​for​​ ​​i​​ ​​in​​ ​​range(len(values)):
 ...​​ ​​values[i]​​ ​​=​​ ​​values[i]​​ ​​*​​ ​​2
 ...
 >>>​​ ​​values
 [8, 20, 6, 16, -12]

Evaluation of the expression on the right side of the assignment looks up the value at index i and multiplies it by two. Python then assigns that value to the item at index i in the list. When i refers to 1, for example, values[i] refers to 10, which is multiplied by 2 to produce 20. The list item values[1] is then assigned 20.

Processing Parallel Lists Using Indices

Sometimes the data from one list corresponds to data from another. For example, consider these two lists:

 >>>​​ ​​metals​​ ​​=​​ ​​[​​'Li'​​,​​ ​​'Na'​​,​​ ​​'K'​​]
 >>>​​ ​​weights​​ ​​=​​ ​​[6.941,​​ ​​22.98976928,​​ ​​39.0983]

The item at index 0 of metals has its atomic weight at index 0 of weights. The same is true for the items at index 1 in the two lists, and so on. These lists are parallel lists, because the item at index i of one list corresponds to the item at index i of the other list.

We would like to print each metal and its weight. To do so, we can loop over each index of the lists, accessing the items in each:

 >>>​​ ​​metals​​ ​​=​​ ​​[​​'Li'​​,​​ ​​'Na'​​,​​ ​​'K'​​]
 >>>​​ ​​weights​​ ​​=​​ ​​[6.941,​​ ​​22.98976928,​​ ​​39.0983]
 >>>​​ ​​for​​ ​​i​​ ​​in​​ ​​range(len(metals)):
 ...​​ ​​print(metals[i],​​ ​​weights[i])
 ...
 Li 6.941
 Na 22.98976928
 K 39.0983

This code works only when the length of weights is at least as long as the length of metals. If the length of weights is less than the length of metals, then an error would occur when trying to access an index of weights that doesn’t exist. For example, if metals has three items and weights has only two, the first two print function calls would be executed, but during the third function call, an error would occur when evaluating the second argument.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.114.85