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


Groups > comp.lang.python > #65113

Re: how to iterate all sub-element in a list

From Peter Otten <__peter__@web.de>
Subject Re: how to iterate all sub-element in a list
Date 2014-01-31 11:48 +0100
Organization None
References <412b4d42-acba-4609-bd6d-e4d5c32db035@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.6202.1391165287.18130.python-list@python.org> (permalink)

Show all headers | View raw


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().

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web