Exercise: Indexing lists 2

3.6.2.5. Exercise: Indexing lists 2#

You are happy with the function calculate_step_length you wrote in the previous exercise , but sometimes, you obtain an IndexError:

# y-coordinates of each heel strike, in meters
y = [0.13, 0.72, 1.29, 1.93, 2.55, 3.12, 3.71, 4.34, 4.95, 5.56]

calculate_step_length(y, 9)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[3], line 4
      1 # y-coordinates of each heel strike, in meters
      2 y = [0.13, 0.72, 1.29, 1.93, 2.55, 3.12, 3.71, 4.34, 4.95, 5.56]
----> 4 calculate_step_length(y, 9)

Cell In[2], line 2, in calculate_step_length(y, step)
      1 def calculate_step_length(y, step):
----> 2     return y[step + 1] - y[step]

IndexError: list index out of range

Question 1) Explain why this call produces an IndexError.

This happens because to calculate a step length, we need two consecutive heel strikes. To calculate the length of step 9, we need heel strikes 9 and 10. However, the last element of y is at index 9.

One way to avoid this error is to check that we won’t index the list out of its range, before calculating the step length.

Question 2) Modify your function so that instead of producing this error, it simply returns 0.

Good practice: Invalid data

For this example, we ask to return 0 for invalid data, but a better practice would be to return np.nan, which will be seen in section Infinity and Not-A-Number (nan). Simply return 0 for now since we didn’t see NumPy yet.

Hide code cell content
def calculate_step_length(y, step):
    """
    Calculate the step length of a given step.

    Parameters
    ----------
    y : list[float]
        A list of every heel strike's y coordinates for a given recording.
    step : int
        The index of the step we want to calculate, the first step being 0.

    Returns
    -------
    float
        The requested step length, or 0 if this step could not be calculated.
    """
    if step + 1 < len(y):
        return y[step + 1] - y[step]
    else:
        return 0.0


# Test it
print(calculate_step_length(y, 0))
print(calculate_step_length(y, 1))
print(calculate_step_length(y, 2))
print(calculate_step_length(y, 9))
0.59
0.5700000000000001
0.6399999999999999
0.0