Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unpacking lists inside another list python [duplicate]

Assume you have a list :

mylist=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]

any pythonic(2.x) way to unpack the inner lists so that new list should look like ?:

mylist_n=[1,2,3,4,2,3,4,5,3,4,5,6]
like image 840
Krcn U Avatar asked Nov 17 '25 05:11

Krcn U


2 Answers

import itertools
mylist=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
print list(itertools.chain(*mylist))
like image 184
Cody Bouche Avatar answered Nov 18 '25 17:11

Cody Bouche


mylist_n = [j for i in mylist for j in i]
like image 35
John La Rooy Avatar answered Nov 18 '25 18:11

John La Rooy



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!