Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenating sublists python [duplicate]

Tags:

python

list

I have one list like: n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]

I want to create a function that takes a single list (see above) and concatenates all the sublists that are part of it into a single list.

like image 984
user2469891 Avatar asked Dec 07 '25 23:12

user2469891


2 Answers

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
nn = [ x for y in n for x in y]
like image 194
perreal Avatar answered Dec 10 '25 13:12

perreal


>>> lst = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
>>> from itertools import chain
>>> list(chain.from_iterable(lst))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
like image 30
Jared Avatar answered Dec 10 '25 12:12

Jared



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!