PAR
PAR

Reputation: 654

How to combine nested lists based on their index?

Consider these nested lists:

L1 = [[1,2,3],[4,5,6],[7,8,9]]
L2 = [[11,22,33],[44,55,66]]
L3 = [[111,222,333],[444,555,666],[777,888,999]]
L4 = [12,13,14] # Note: not a nested list

I require:

output1 = [1,4,7,11,44,111,444,777,12]
output2 = [2,5,8,22,55,222,555,888,13]
output3 = [3,6,9,33,66,333,666,999,14]

Upvotes: 1

Views: 268

Answers (4)

Gormador
Gormador

Reputation: 380

A oneliner:

output1, output2, output3 = [[l[i] for l in L1+L2+L3+[L4]] for i in range(3)]

Outputs:

>>> output1
[1, 4, 7, 11, 44, 111, 444, 777, 12]
>>> output2
[2, 5, 8, 22, 55, 222, 555, 888, 13]
>>> output3
[3, 6, 9, 33, 66, 333, 666, 999, 14]

Though you really should provide a sample of what you tried when asking for a solution :-)

Update

As we are apparently indulging ourselves with a friendly competition, here's the timing of different answers on my machine (python3):

Code:

import time

L1 = [[1,2,3],[4,5,6],[7,8,9]]
L2 = [[11,22,33],[44,55,66]]
L3 = [[111,222,333],[444,555,666],[777,888,999]]
L4 = [12,13,14]


t = time.process_time()

output1, output2, output3 = [[l[i] for l in L1+L2+L3+[L4]] for i in range(3)]

print(output1)
print(output2)
print(output3)

print("%.7f" %(time.process_time() - t))


t = time.process_time()

output1, output2, output3 = map(list,(zip(*L1+L2+L3+[L4])))

print(output1)
print(output2)
print(output3)

print("%.7f" %(time.process_time() - t))


t = time.process_time()

from itertools import chain

output_1, output_2, output_3 = zip(*chain(L1, L2, L3, [L4]))

print(output1)
print(output2)
print(output3)

print("%.7f" %(time.process_time() - t))

Results (of one of the fastest runs):

[1, 4, 7, 11, 44, 111, 444, 777, 12]
[2, 5, 8, 22, 55, 222, 555, 888, 13]
[3, 6, 9, 33, 66, 333, 666, 999, 14]
0.0000650
[1, 4, 7, 11, 44, 111, 444, 777, 12]
[2, 5, 8, 22, 55, 222, 555, 888, 13]
[3, 6, 9, 33, 66, 333, 666, 999, 14]
0.0000356
[1, 4, 7, 11, 44, 111, 444, 777, 12]
[2, 5, 8, 22, 55, 222, 555, 888, 13]
[3, 6, 9, 33, 66, 333, 666, 999, 14]
0.0002259

@Chris_Rands answer seems to be the fastest :-)

Upvotes: 3

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48090

I am amazed that no one has mentioned the usage of itertools.chain() with zip(). The simplest solution will be:

from itertools import chain

output_1, output_2, output_3 = zip(*chain(L1, L2, L3, [L4]))

which make variables hold the value as:

>>> output_1
(1, 4, 7, 11, 44, 111, 444, 777, 12)
>>> output_2
(2, 5, 8, 22, 55, 222, 555, 888, 13)
>>> output_3
(3, 6, 9, 33, 66, 333, 666, 999, 14)

Upvotes: 1

Chris_Rands
Chris_Rands

Reputation: 41188

There is a simple solution with zip:

output1, output2, output3 = map(list,(zip(*L1+L2+L3+[L4])))

Note that if you are content with tuples instead of lists, this can be further simplified:

output1, output2, output3 = zip(*L1+L2+L3+[L4])

I found this to be slightly faster than using chain to merge the lists, at least for this data. It also appears to be faster than @Gormador's solution.

>>> def f1():
...     output1, output2, output3 = zip(*L1+L2+L3+[L4])
... 
>>> def f2():
...     output_1, output_2, output_3 = zip(*chain(L1, L2, L3, [L4]))
... 
>>> def f3():
...     output1, output2, output3 = [[l[i] for l in L1+L2+L3+[L4]] for i in range(3)]
... 
>>> timeit(lambda: f1(), number = 1000000)
1.5090796248987317
>>> timeit(lambda: f2(), number = 1000000)
1.7326991918962449
>>> timeit(lambda: f3(), number = 1000000)
5.100052359048277

Upvotes: 2

phynfo
phynfo

Reputation: 4938

Try this:

L = L1+L2+L3+[L4]
output1 = [l[0] for l in L]
output2 = [l[1] for l in L]
output3 = [l[2] for l in L]

Upvotes: 0

Related Questions