Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65108 > unrolled thread

how to iterate all sub-element in a list

Started byseaspeak@gmail.com
First post2014-01-31 02:16 -0800
Last post2014-01-31 04:12 -0800
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  how to iterate all sub-element in a list seaspeak@gmail.com - 2014-01-31 02:16 -0800
    Re: how to iterate all sub-element in a list Skip Montanaro <skip@pobox.com> - 2014-01-31 04:29 -0600
      Re: how to iterate all sub-element in a list seaspeak@gmail.com - 2014-01-31 02:42 -0800
        Re: how to iterate all sub-element in a list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-31 14:48 +0000
          Re: how to iterate all sub-element in a list seaspeak@gmail.com - 2014-01-31 08:23 -0800
    Re: how to iterate all sub-element in a list Peter Otten <__peter__@web.de> - 2014-01-31 11:48 +0100
      Re: how to iterate all sub-element in a list Rustom Mody <rustompmody@gmail.com> - 2014-01-31 04:12 -0800

#65108 — how to iterate all sub-element in a list

Fromseaspeak@gmail.com
Date2014-01-31 02:16 -0800
Subjecthow to iterate all sub-element in a list
Message-ID<412b4d42-acba-4609-bd6d-e4d5c32db035@googlegroups.com>
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.

[toc] | [next] | [standalone]


#65110

FromSkip Montanaro <skip@pobox.com>
Date2014-01-31 04:29 -0600
Message-ID<mailman.6201.1391164607.18130.python-list@python.org>
In reply to#65108
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

[toc] | [prev] | [next] | [standalone]


#65111

Fromseaspeak@gmail.com
Date2014-01-31 02:42 -0800
Message-ID<600f2d6b-8068-453c-b21f-b23d0b13ff62@googlegroups.com>
In reply to#65110
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.

[toc] | [prev] | [next] | [standalone]


#65121

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-01-31 14:48 +0000
Message-ID<mailman.6208.1391179741.18130.python-list@python.org>
In reply to#65111
On 31/01/2014 10:42, seaspeak@gmail.com wrote:
> 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.
>

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

[toc] | [prev] | [next] | [standalone]


#65132

Fromseaspeak@gmail.com
Date2014-01-31 08:23 -0800
Message-ID<578fc71b-d06c-47fd-ba65-564864239202@googlegroups.com>
In reply to#65121
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.

[toc] | [prev] | [next] | [standalone]


#65113

FromPeter Otten <__peter__@web.de>
Date2014-01-31 11:48 +0100
Message-ID<mailman.6202.1391165287.18130.python-list@python.org>
In reply to#65108
seaspeak@gmail.com 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().

[toc] | [prev] | [next] | [standalone]


#65116

FromRustom Mody <rustompmody@gmail.com>
Date2014-01-31 04:12 -0800
Message-ID<c972056c-dd6a-40f6-ba11-0afa3815d49c@googlegroups.com>
In reply to#65113
On Friday, January 31, 2014 4:18:13 PM UTC+5:30, Peter Otten wrote:
> 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]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web