how to iterate all sub-element in a list

S

seaspeak

a list like L = [[1, 2], [3, 4, 5], [6]], which has
random numbers of list, which has random numbers.

How do I get another list m = [1, 2, 3, 4, 5, 6] in a succinct way? ( or iterate them)
I should provide my own solution, but I really can't come out with one.
 
S

Skip Montanaro

Google for "python flatten list."

This question comes up frequently, though I'm not sure if that's
because it's a common homework problem or because people are unaware
of the += operator (or extend method) for lists, and so build
lists-of-lists when they could just build them flat in the first
place.

Skip
 
S

seaspeak

Skip Montanaroæ–¼ 2014å¹´1月31日星期五UTC+8下åˆ6時29分27秒寫é“:
Google for "python flatten list."



This question comes up frequently, though I'm not sure if that's

because it's a common homework problem or because people are unaware

of the += operator (or extend method) for lists, and so build

lists-of-lists when they could just build them flat in the first

place.



Skip
thanks. a keyword is all I need.
It is not homework. It is a common problem I guess.
 
P

Peter Otten

a list like L = [[1, 2], [3, 4, 5], [6]], which has
random numbers of list, which has random numbers.

How do I get another list m = [1, 2, 3, 4, 5, 6] in a succinct way? ( or
iterate them) I should provide my own solution, but I really can't come
out with one.

sum(L, [])
list(itertools.chain.from_iterable(L))

but as a learning experience you should do this at least once with two
nested for loops, then again with a single for loop and list.extend().
 
R

Rustom Mody

seaspeak wrote:
a list like L = [[1, 2], [3, 4, 5], [6]], which has
random numbers of list, which has random numbers.
How do I get another list m = [1, 2, 3, 4, 5, 6] in a succinct way? ( or
iterate them) I should provide my own solution, but I really can't come
out with one.
sum(L, [])
list(itertools.chain.from_iterable(L))
but as a learning experience you should do this at least once with two
nested for loops, then again with a single for loop and list.extend().

And then again with a double comprehension:
[y for x in L for y in x]
 
M

Mark Lawrence

Skip Montanaroæ–¼ 2014å¹´1月31日星期五UTC+8下åˆ6時29分27秒寫é“:
thanks. a keyword is all I need.
It is not homework. It is a common problem I guess.

I'm pleased to see that you have answers. In return would you please
read and action this https://wiki.python.org/moin/GoogleGroupsPython to
prevent us seeing the double line spacing above, thanks.
 
S

seaspeak

Mark Lawrenceæ–¼ 2014å¹´1月31日星期五UTC+8下åˆ10時48分46秒寫é“:
I'm pleased to see that you have answers. In return would you please
read and action this https://wiki.python.org/moin/GoogleGroupsPython to
prevent us seeing the double line spacing above, thanks.
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence

My bad, I replied in a hurry with my mobile.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top