Python: Calculating difference of values in a nested list by using a While Loop
<p>I have a list that is composed of nested lists, each nested list contains two values - a float value (file creation date), and a string (a name of the file). </p>

<p>For example: </p>

<pre><code>n_List = [[201609070736L, 'GOPR5478.MP4'], [201609070753L, 'GP015478.MP4'],[201609070811L, 'GP025478.MP4']]
</code></pre>

<p>The nested list is already sorted in order of ascending values (creation dates). I am trying to use a While loop to calculate the difference between each sequential float value. </p>

<p>For Example: 201609070753 - 201609070736 = 17</p>

<p>The goal is to use the time difference values as the basis for grouping the files. </p>

<p>The problem I am having is that when the count reaches the last value for <code>len(n_List)</code> it throws an <code>IndexError</code> because <code>count+1</code> is out of range. </p>

<pre><code>IndexError: list index out of range
</code></pre>

<p>I can't figure out how to work around this error. no matter what i try the count is always of range when it reaches the last value in the list. </p>

<p>Here is the While loop I've been using. </p>

<pre><code>count = 0

while count &lt;= len(n_List):
    full_path = source_folder + "/" + n_List[count][1]

    time_dif = n_List[count+1][0] - n_List[count][0]


    if time_dif &lt; 100:  
       f_List.write(full_path + "\n")
       count = count + 1 
    else: 
        f_List.write(full_path + "\n")
        f_List.close()
        f_List = open(source_folder + 'GoPro' + '_' + str(count) + '.txt', 'w')
        f_List.write(full_path + "\n")
        count = count + 1
</code></pre>

<p>PS. The only work around I can think of is to assume that the last value will always be appended to the final group of files. so, when the count reaches <code>len(n_List - 1)</code>, I skip the time dif calculation, and just automatically add that final value to the last group. While this will probably work most of the time, I can see edge cases where the final value in the list may need to go in a separate group. </p>

<p>n_list(len(n_list)) will always return an index out of range error</p>

<pre><code>while count &lt; len(n_List):
</code></pre>

<p>should be enough because you are starting count at 0, not 1.</p>

python list while-loop nested