Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do do this list manipulation in Python? This is tricky

Tags:

python

list

Suppose I have this list:

[ [5, 44, 73] , [7, 21, 99], [1, 32, 100] ]

What is the MOST efficient way to turn it into this list?

[ 5, 7, 1, 44, 21, 32, 73, 99, 100 ] 

Notice, I grab the first from each. Then the 2nd element from each. Of course, this function needs to be done with X elements.

I've tried it, but mine has many loops,and I think it's too long and complicated.

Thanks.

like image 281
TIMEX Avatar asked Jul 04 '26 11:07

TIMEX


1 Answers

>>> L1 =  [ [5, 44, 73] , [7, 21, 99], [1, 32, 100] ]
>>> L2 = []
>>> map(L2.extend, zip(*L1))
>>> L2
[5, 7, 1, 44, 21, 32, 73, 99, 100]
like image 122
mthurlin Avatar answered Jul 07 '26 00:07

mthurlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!