Obtaining the first and second "column's" from a pair of lists

1k Views Asked by At

I have many pairs of lists of variable lengths (5,4,6 pairs etc..) inside a single big list, lets call it LIST. Here are two lists among the many inside the big LIST as an example:

[(38.621833, -10.825707),
 (38.572191, -10.84311),      -----> LIST[0]
 (38.580202, -10.860877),
 (38.610917, -10.85217),
 (38.631526, -10.839338)]

[(38.28152, -10.744559),
 (38.246368, -10.744552),     -----> LIST[1]
 (38.246358, -10.779088),
 (38.281515, -10.779096)]

I need to create two seperate variables lets say, of which one variable will have the first "column" (i.e. LIST[0][0][0], LIST[0][1][0] AND SO ON) of all the pairs of the lists(i.e. 38.621833, 38.572191 etc) and the second variable will have the second "column" (i.e. LIST[0][0][1], LIST[0][1][1] AND SO ON) of all the pairs of the lists.

So finally I will have two variables (say x,y) that will contain all the values of the first and second "columns" of all the lists in the LIST.

The problem I face is that all these lists are not of the same length!!

I tried

x = []
y = []
for i in range(len(LIST)):
    x.append(LIST[i][0][0]) #append all the values of the first numbers 
    y.append(LIST[i][1][1]) #append all the values of the second numbers

What I expect:

x = (38.621833,38.572191,38.580202,38.610917,38.631526,38.28152,38.246368,38.246358,38.281515)

y = (-10.825707,-10.84311,-10.860877,-10.85217,-10.839338,-10.744559,-10.744552,-10.779088,-10.779096)

But here because of the variable pairs, my loop stops abrubptly in between. I know I need to also change the LIST[i][j][0] here, and j changes with each list. But because of the different pairs, I don't know how to go about.

How do I go about doing this?

5

There are 5 best solutions below

4
On BEST ANSWER

I would use two simple for loops (it's also generic for LIST being longer than 2):

x=[]
y=[]
for i in range(len(LIST)):
    for j in LIST[i]:
        x.append(j[0])
        y.append(j[1])
3
On

Use map function with zip and * stuple operator.

l = [(38.621833, -10.825707),
 (38.572191, -10.84311),      
 (38.580202, -10.860877),
 (38.610917, -10.85217),
 (38.631526, -10.839338)]
x= map(list, zip(*l))[0]
y = map(list, zip(*l))[1]
print 'x = {},\n y = {}' .format(x,y)

 x = [38.621833, 38.572191, 38.580202, 38.610917, 38.631526],
 y = [-10.825707, -10.84311, -10.860877, -10.85217, -10.839338]

or if you don't want to store it in variables then d0n't use indexing in above solution,

map(list, zip(*l)) # will give you a nested list
1
On

Here you go. tuple as requested.

my = [(38.621833, -10.825707),(38.572191, -10.84311),(38.580202, -10.860877),(38.610917, -10.85217),(38.631526, -10.839338)]

my1 = [(38.28152, -10.744559),(38.246368, -10.744552),(38.246358, -10.779088),(38.281515, -10.779096)]



l1 = map(tuple,zip(*my))[0]
l2 = map(tuple,zip(*my))[1]
print l1,l2

Output:

(38.621833, 38.572191, 38.580202, 38.610917, 38.631526)(-10.825707, -10.84311, -10.860877, -10.85217, -10.839338)
4
On

You should transpose the sublists and use itertool.chain to create a single list:

from itertools import chain
zipped = [zip(*x) for x in l]
x, y = chain.from_iterable(ele[0] for ele in  zipped),chain.from_iterable(ele[1] for ele in  zipped)
print(list(x),list(y))


[38.621833, 38.572191, 38.580202, 38.610917, 38.631526, 38.28152, 38.246368, 38.246358, 38.281515] [-10.825707, -10.84311, -10.860877, -10.85217, -10.839338, -10.744559, -10.744552, -10.779088, -10.779096]


for ele1,ele2 in zip(x,y):
    print(ele1,ele2)

38.621833 -10.825707
38.572191 -10.84311
38.580202 -10.860877
38.610917 -10.85217
38.631526 -10.839338
38.28152 -10.744559
38.246368 -10.744552
38.246358 -10.779088
38.281515 -10.779096
0
On

Your LIST extends out of 2 lists. With

for i in range(len(LIST)):

you run exactly 2 times through your loop.

If you want to solve your problem with for-loops your need to nest them:

#declare x, y as lists
x = []
y = []
for i_list in LIST:
    #outer for-loop runs 2 times - one for each list appended to LIST.
    #1st run: i_list becomes LIST[0]
    #2nd run: i_list becomes LIST[1]
    for touple in i_list:
        #inner for-loop runs as often as the number of tuple appended to i_list
        #touple becomes the content of i_list[#run]
        x.append(touple[0]) #adds x-value to x
        y.append(touple[1]) #adds y-value to y

If you prefer working with indexes use:

for i in range(len(LIST)):
    for j in range(len(LIST[i])):
        x.append(LIST[i][j][0])
        y.append(LIST[i][j][1]])

NOT working with indexes for appending x- or y-values is much easier to write (saves complex thoughts about the List-Structure and correct using of indexes) and is much more comprehensible for extern people reading your code.