For loop to append all numbers to variable

I wrote some code to open a text file and take the columns that I need and assign it to a variable. Everything before the for loop is OK. Now inside the loop, I want to have all numbers in g as a list of doubles, but all the time I have only last number of lis1.

Thanks in advance.

Codes:

import numpy as N
import math as M

with open('File.txt',"r") as f:
    lis1 = [float(line.split()[4]) for line in f]
    f.seek(0)
    lis2 = [float(line.split()[6]) for line in f]
    f.seek(0)
    lis3 = [float(line.split()[8]) for line in f]
    f.seek(0)
    lis4 = [float(line.split()[10]) for line in f]
    f.seek(0)
    lis5 = [float(line.split()[12]) for line in f]
    f.seek(0)
    lis6 = [float(line.split()[14]) for line in f]
    i= 0
    j = i+1

    for t in (lis1):
        g = ((lis1[i:j]))                                   
        k = ((lis2[i:j]))                                          
        kl = ((lis3[i:j]))                                    
        kk = ((lis4[i:j]))
        kk2 = ((lis5[i:j]))
        kk3 = ((lis6[i:j]))
        i = i+1
        j = j+1

The problem is that the g variable is updated in each iteration.

At last if for t loops 10 you should have g = ((list1[10:11]))

Example:

list1=[0,1,2,3,4,5,6,7,8,9]
i=0  
j=i+1  
for t in list1:  
    g=((list1[i:j]))  
    i+=1  
    j+=1  
...
g = (([9]))

We should have to get from the last index to .., which will return the last element.


Your problem is that in each run of the loop you assign g=lis1[i:j] which will make g end up with the valuelis1[i:j] have in the loop (which will only be the last element of lis1).

If you want g to contain all elements of lis1 you only have to copy lis1:

import numpy as N
import math as M

with open('File.txt',"r") as f:
    lis1 = [float(line.split()[4]) for line in f]
    f.seek(0)
    lis2 = [float(line.split()[6]) for line in f]
    f.seek(0)
    lis3 = [float(line.split()[8]) for line in f]
    f.seek(0)
    lis4 = [float(line.split()[10]) for line in f]
    f.seek(0)
    lis5 = [float(line.split()[12]) for line in f]
    f.seek(0)
    lis6 = [float(line.split()[14]) for line in f]

    g = []

    for t in (lis1):
        g.append(t)                          

But then of course one would wonder why you want to do that to begin with (you already have that in lis1) - you could do this easier by copying directly as g=lis1[:] or even g=lis1.