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


Groups > comp.lang.python > #51118

Re: Converting a list of lists to a single list

References <ee6f5c48-6cb1-498f-813a-be2eef5411fc@googlegroups.com> <51EF04F7.9040401@gmail.com>
Date 2013-07-24 15:40 +1000
Subject Re: Converting a list of lists to a single list
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5025.1374644439.3114.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Jul 24, 2013 at 8:34 AM, Rafael Durán Castañeda
<rafadurancastaneda@gmail.com> wrote:
> In [3]: [ y for y in itertools.chain.from_iterable(x)]
> Out[3]: ['A0', 'A1', 'A2', 'B0', 'B1', 'B2', 'C0', 'C1', 'C2']

Complete aside, given that this has already been pointed out as
solving a different problem: Any time you see a list comp that just
does "[ x for x in foo ]", check to see if it can be replaced by a
simple list constructor:

>>> [ y for y in itertools.chain.from_iterable(x)]
['A0', 'A1', 'A2', 'B0', 'B1', 'B2', 'C0', 'C1', 'C2']
>>> list(itertools.chain.from_iterable(x))
['A0', 'A1', 'A2', 'B0', 'B1', 'B2', 'C0', 'C1', 'C2']

A bit simpler and achieves the same.

ChrisA

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


Thread

Converting a list of lists to a single list steve@divillo.com - 2013-07-23 14:52 -0700
  Re: Converting a list of lists to a single list Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2013-07-24 00:34 +0200
  Re: Converting a list of lists to a single list MRAB <python@mrabarnett.plus.com> - 2013-07-23 23:52 +0100
  Re: Converting a list of lists to a single list Zero Piraeus <schesis@gmail.com> - 2013-07-23 18:49 -0400
  Re: Converting a list of lists to a single list Terry Reedy <tjreedy@udel.edu> - 2013-07-23 19:02 -0400
  Re: Converting a list of lists to a single list Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-07-23 20:28 -0400
  Re: Converting a list of lists to a single list Chris Angelico <rosuav@gmail.com> - 2013-07-24 15:40 +1000
  Re: Converting a list of lists to a single list Terry Reedy <tjreedy@udel.edu> - 2013-07-24 11:56 -0400
  Re: Converting a list of lists to a single list steve@divillo.com - 2013-07-24 11:11 -0700

csiph-web