Check your understanding of the previous reading: value style and index style, the enumerate, range and len functions, and updating list elements by index.

Question 1

Which of the following is the easiest way to print each element of a list in Python?

Select one answer, then check.

  • Not quite. This works, but the index style needs two extra functions, range and len, and an indexing operation for every element.
  • Correct. The value style iterates over the elements directly, with no indexes to manage; it is the usual way to process a list in Python.
  • Not quite. This works, but it keeps its own index i next to an iteration variable it never uses: the most work of the three.

Question 2

Which of the following are reasons to avoid using indexes to process a list?

Select every answer that applies, then check.

  • True. It needs range and len (or enumerate) and an indexing operation for every element.
  • True. Indexes are more complex to reason about, especially when they are used to change the list.
  • Not true. range(len(a_list)) produces every index of the list, so no element is skipped.
  • Not true. A single loop over range(len(a_list)) visits every index.

Question 3

True or false: You should ALWAYS avoid using index iteration to process a list. There are no advantages to it.

Select one answer, then check.

  • Not quite. Indexes allow more flexibility and are necessary for certain kinds of algorithms, for example to update the original list or to reach an adjacent element.
  • Correct. Stick to the value style unless you have a reason to use indexes, but indexes do allow more flexibility: you need them to update the original list or to reach an adjacent element.

Question 4

The enumerate function consumes a list and returns each element paired with its index. When the following code is run, what is printed on the first line?

values=[1,2,3]
for i, value in enumerate(values):
    print(i, value)

If you are not sure, consider trying the code out above - you don't have to guess!

Show worked solution

enumerate(values) pairs each element with its index, and indexes start at 0. On the first pass i is 0 and value is 1, so print(i, value) prints 0 1.

Question 5

The range function consumes an integer and can be used to produce a list of numbers from 0 to that integer. When the following code is run, what is printed on the LAST line?

for value in range(10):
    print(value)

If you are not sure, consider trying the code out above - you don't have to guess!

Show worked solution

range(10) produces the numbers from 0 up to, but not including, 10. The loop prints them one per line, so the last line is 9.

Question 6

The range and len functions can be combined to iterate through indices. When the following code is run, what is printed FIRST? Pay close attention to the code inside the for loop!

values=[6, 7, 8]
for index in range(len(values)):
    print(values[index-1])

If you are not sure, consider trying the code out above - you don't have to guess!

Show worked solution

On the first pass index is 0, so the loop prints values[0-1], which is values[-1]: the last element, 8.

Question 7

The following code is executed:

data = [4, 6, 8]

for index, value in enumerate(data):
    data[index] = data[index] + 1
    value = value * 2

data[1] = 8

Run the code above and evaluate each expression:

1. data[0]:

2. data[1]:

3. value:

Word bank — not all values are used

Question 8

When this code is executed, what will be printed?

data = [4, 6, 8]
data[2], data[0] = [data[0], data[2]]
print(data)

Select one answer, then check.

  • Not quite. Assigning a list of two values to two indexes is valid Python; it is how the reading swaps two elements.
  • Not quite. The assignment does change the list: both indexes on the left-hand side get a new value.
  • Not quite. Index 1 is not on the left-hand side, so the 6 stays in the middle.
  • Not quite. Only indexes 2 and 0 are assigned; the 6 at index 1 doesn't move.
  • Not quite. Only indexes 2 and 0 are assigned; the 6 at index 1 doesn't move.
  • Not quite. Only indexes 2 and 0 are assigned; the 6 at index 1 doesn't move.
  • Correct. The right-hand side is evaluated first: [data[0], data[2]] is [4, 8]. Then data[2] becomes 4 and data[0] becomes 8, so the first and last elements swap.