Workshop 4: Nested LoopsΒΆ

Let's start with a simple task. Suppose we have heartrate data that's organized as follows:

In [1]:
#in row i we have data for setting i
#in column j we have data for subject j
hr = [ [ 72,   75,  71,  73, 0],   # resting
       [ 91,   90,  94,  93, 0],   # walking slowly
       [ 130, 135, 139, 142, 0],   # running on treadmill
       [ 120, 118, 110, 105, 0]]   # after minute recovery

Suppose we want to get the average for every subject. (That is, we want the average for each column.) First, let's write a function that computes the average for column col_ind.

In [2]:
def get_col_avg(data, col_ind):
    '''Return the average of column col_ind in a the
    nested list data'''
    s = 0
    for setting in data:
        s += setting[col_ind]
    return s/len(data)

Now, we can get the average for each subject/column using a for-loop:

In [3]:
col_avgs = []
for col_ind in range(len(hr[0])):
    col_avgs.append(get_col_avg(hr, col_ind))

Here's a different approach: make a nested loop -- a loop within a loop

In [5]:
subj_avgs = []
for subject_ind in range(len(hr[0])):
    s = 0
    for setting_ind in range(len(hr)):
        s += hr[setting_ind][subject_ind]

    subj_avgs.append(s/len(hr))
In [6]:
from IPython.display import YouTubeVideo
YouTubeVideo("7i4KTI6QOfA")
Out[6]:

Here is something that is more directly analguous to the approach we took when we called get_call_avg:

In [7]:
subj_avgs = []
for subject_ind in range(len(hr[0])):
    s = 0
    for setting in hr:
        s += setting[subject_ind]
    subj_avgs.append(s/len(hr))

As you can see, nested loops -- loops within loops -- can be avoided by instead calling functions (which contain loops) from within loops.