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>FYI, here is the solution I used, thanks to @galaxyman for the help.</p>

<p>I handled the issue of the last value in the nested list, by simply
adding that value after the loop completes. Don't know if that's the most
elegant way to do it, but it works. </p>

<p>(note: i'm only posting the function related to the zip method suggested in the previous posts). </p>

<pre><code>    def list_zip(get_gp_list): 
        ffmpeg_list = open(output_path + '\\' + gp_List[0][1][0:8] + '.txt', 'a')
        for a,b in zip(gp_List,gp_List[1:]):
            full_path = gopro_folder + '\\' + a[1]
            time_dif = b[0]-a[0]
            if time_dif &lt; 100: 
                ffmpeg_list.write("file " + full_path + "\n")
            else:
                ffmpeg_list.write("file " + full_path + "\n")
                ffmpeg_list.close()
                ffmpeg_list = open(output_path + '\\' + b[1][0:8] + '.txt', 'a') 

    last_val = gp_List[-1][1]
    ffmpeg_list.write("file " + gopro_folder + '\\' + last_val + "\n")
    ffmpeg_list.close()
</code></pre>

python list while-loop nested