1

I have a loop that gives me a list of lists of integers like the example bellow:

l1 = [[1,2],[7,8],[13,14],[19,20]]
l2 = [[3,4],[9,10],[15,16],[21,22]]
l3 = [[5,6],[11,12],[17,18],[23,24]]

The result I need is like a vertical extension of the lists. So the result should be:

l_final = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,24]]

Assuming that I get those lists in a loop one by one, I think I cannot use all of them at once, so a solution like some_function(l1,l2,l3) is out of the scope. Ideally, the solution would work in this structure:

l_final = []
for loop gives me one listOFlists:
    l1 = [[1,2],[7,8],[13,14],[19,20]]

    l_final = # solution someone? :/

Could somebody help me, please?

Fjord
  • 144
  • 1
  • 9

5 Answers5

3

Use zip(l1, l2, l3) to get a sequence of tuples like ([1, 2], [3, 4], [5, 6]) and then use this solution to flatten them:

[[item for sublist in x for item in sublist] for x in zip(l1, l2, l3)]

or more generally (thanks for the suggestion akasolace), you can use *arg notation to unpack the listOFlists into arguments for the zip function:

[[item for sublist in x for item in sublist] for x in zip(*listOFlists)]

gives:

[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]]
alani
  • 12,573
  • 2
  • 13
  • 23
  • this answer is really good but to make it more general (not only 3 lists) you can do following your example: l_final = [[item for sublist in x for item in sublist] for x in zip(*listOFlists)] – akasolace Jul 31 '20 at 11:22
  • @akasolace Now done - I've credited you for suggesting it. – alani Jul 31 '20 at 11:26
  • @akasolace I see there are now several answers including from yourself. All seem to me useful - it's nice to see the variety of methods - though I'm not completely convinced about the numpy answer because it assumes that lengths will be equal. – alani Jul 31 '20 at 11:31
  • Good one, alaniwi! But akasolace's solution fits better in my case. I upvoted you anyway, thanks – Fjord Jul 31 '20 at 11:36
  • @alaniwi, yes I am a modest contributor and I am always amazed by the general quality of answers provided. In that example, I answered but I voted for yours that I preferred to mine :-) – akasolace Jul 31 '20 at 11:36
2
l1 = [[1,2],[7,8],[13,14],[19,20]]
l2 = [[3,4],[9,10],[15,16],[21,22]]
l3 = [[5,6],[11,12],[17,18],[23,24]]

listOFlists = [l1, l2, l3]



l_final = []
for l in listOFlists:
    for i, sublist in enumerate(l):
        if len(l_final) >= i+1:
            l_final[i].extend(sublist)
        else:
            l_final.append(sublist.copy())

print(l_final)

#[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]]
akasolace
  • 572
  • 1
  • 5
  • 17
2

You can use zip to group the sublists and then use itertools.chain to flatten it

>>> from itertools import chain
>>> lst = [l1, l2, l3]
>>> [list(it.chain(*l)) for l in zip(*lst)]
[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]]
>>> 
Prem Anand
  • 2,469
  • 16
  • 16
1

you can use zip to get elements from each of the iterables and unpack the i, j, k inside a list using * operator.

l_final = [[*i, *j, *k] for i, j, k in zip(l1, l2, l3)]

Output:

[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]]
Vishal Singh
  • 6,014
  • 2
  • 17
  • 33
  • this answer is really good but to make it more general (not only 3 lists) you can do following your example: l_final = [[item for sublist in x for item in sublist] for x in zip(*listOFlists)] – akasolace Jul 31 '20 at 11:20
  • your suggestion is very good but that is already included in @alaniwi answer. – Vishal Singh Jul 31 '20 at 11:32
  • yes sorry I wanted to put that comment in his answer but picked up the wrong one (but your answer is also good :-)) – akasolace Jul 31 '20 at 11:33
0

Using numpy:

li1 = np.array(l1)
li2 = np.array(l2)
li3 = np.array(l3)
c = np.concatenate((li1,li2,li3),axis=1)

c:

array([[ 1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12],
       [13, 14, 15, 16, 17, 18],
       [19, 20, 21, 22, 23, 24]])
Pygirl
  • 12,969
  • 5
  • 30
  • 43