Skip to main content

Section 27.4 Working with Indexes

Sometimes, we don’t want to access all of the items in a list one at a time in order. To access individual items in a collection (a list or a string), we can use an index. Each item in a collection has a number associated with it – think of it as the item’s address in the collection. The first item in a collection has index 0, the next one 1, and so on. See the image below for a view of two lists with the index for each list item shown at the top of each yellow box and the value for that index shown at the bottom of each yellow box.
Figure 27.4.1. Lists and their indices
We use square brackets to access items of the list, e.g., myList[0] will access the first item in the list. myList[1] will access the second item in the list.

Warning 27.4.2.

In programming, we often count from 0 instead of from 1. It will take some getting used to, but when you see a list of things, remember that the first one is item 0. The last item will always have an index one less than the number of items - the last item in a list with 10 values is index 9.

Checkpoint 27.4.3.

    What is the last index for the list myFirstList?
  • 0
  • This is the index of the first item in the list.
  • 1
  • This is the index of the second item in the list.
  • 2
  • This is the index of the last item in this list since it contains 3 items and the first index is 0.
  • 3
  • The length of this list is 3, but the first index is 0 so the 3rd item is at index 2.

Checkpoint 27.4.4.

    What is the value of the item at index 3 in mySecondList?
  • 12
  • This is the value at index 0.
  • "ape"
  • This is the value at index 1.
  • 13
  • This is the value at index 2.
  • 321.4
  • This is the value at index 3.
You can read and write the values of individual items of a list just like they were variables. Using list[index] on the right side of an assignment returns the value at that index in the list. Using list[index] on the left side of an assignment statement changes the value at that index in the list.

Checkpoint 27.4.5.

    What would the following code print?
    values = [3, 2, 1]
    values[0] = values[1]
    values[2] = values[2] + 1
    print(values)
    
  • [3, 2, 1]
  • That is the original contents of values, but the contents are changed.
  • [2, 0, 2]
  • When you set values[0] to values[1] it makes a copy of the value and doesn’t zero it out.
  • [2, 2, 2]
  • The value at index 0 is set to a copy of the value at index 1 and the value at index 2 is incremented.
  • [2, 2, 1]
  • Notice that we do change the value at index 2. It is incremented by 1.
We can even use a variable that names a number as the index for an item. This sample uses itemNum to identify which value we want from the list. As we change itemNum, the item we access by using it as an index changes as well:
This trick will come in very handy later when we write more complex logic for accessing all of the items in a list.
Finally, just like with a string, we can find the length of a list by using the len function. We can either use it in the same way we would use any other numeric value by doing more work with it or giving the value a name to work with later:
You have attempted of activities on this page.