September 2013
Intermediate to advanced
350 pages
9h 38m
English
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:

Let’s add a call on ...
Read now
Unlock full access