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


Groups > comp.lang.python > #95006

Re: GOTCHA with list comprehension

References <ebf0a31e-1021-41d2-affb-8318838a464b@googlegroups.com> <mailman.1231.1438758358.3674.python-list@python.org> <87wpxacdnr.fsf@elektro.pacujo.net>
Date 2015-08-05 19:52 +1000
Subject Re: GOTCHA with list comprehension
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1234.1438768326.3674.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Aug 5, 2015 at 7:01 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Chris Angelico <rosuav@gmail.com>:
>
>> You can chain 'for' and 'if' clauses as much as you like, and they
>> behave exactly the way you'd expect.
>
> How do you know what I'd expect?
>
> I wouldn't know what to expect myself.

A list comprehension can always be unwound into statement form. [1] For example:

squares = [n*n for n in range(10)]

can be unwound into:

squares = []
for n in range(10):
    squares.append(n*n)

You simply take the expression at the beginning and put that into the
append() method call, and then the rest become statements. Here's a
filtered version:

odd_squares = [n*n for n in range(20) if n%2]

Which becomes:

odd_squares = []
for n in range(20):
    if n%2:
        odd_squares.append(n*n)

So what would you expect nested 'if' clauses to do? Well, they become
nested 'if' statements:

primes = [n for n in range(2,24) if n%2 if n%3]

primes = []
for n in range(2,24):
    if n%2:
        if n%3:
            primes.append(n)

What if we have multiple 'for' loops? Same thing!

ways_to_get_seven = [(a,b) for a in range(1,7) for b in range(1,7) if a+b==7]

ways_to_get_seven = []
for a in range(1,7):
    for b in range(1,7):
        if a+b==7:
            ways_to_get_seven.append((a,b))

No matter what combination of 'if' and 'for' you use, it can be
unwound like this, and it'll always behave the same way. Not sure what
to expect? Follow the simple rules of unwinding, and then read the
code that way.

Of course, if you don't know how to predict what the statement form
will do, then you won't understand what the comprehension will do. But
that's not the comprehension's fault.

peculiar = [a*x*x+b*x+c for a in range(1,10) for b in range(2,30) for
c in range(-3,5) if b*b-4*a*c>=0 if (-b+math.sqrt(b*b-4*a*c))/2/a>0
for x in (x*.01 for x in range(-500,500))]

I suppose you could graph that. Or something. But that's just bad
code, don't blame the list comp for that...

ChrisA

[1] To be technically correct, this unwinding should be done in a
nested function, which you then call. There are some other minor
technicalities too. But it's pretty much this.

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


Thread

GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-04 23:48 -0700
  Re: GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-05 00:03 -0700
    Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 17:20 +1000
  Re: GOTCHA with list comprehension Peter Otten <__peter__@web.de> - 2015-08-05 09:04 +0200
  Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 17:05 +1000
    Re: GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-05 00:10 -0700
      Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 17:21 +1000
        Re: GOTCHA with list comprehension Pavel S <pavel@schon.cz> - 2015-08-05 00:33 -0700
    Re: GOTCHA with list comprehension Marko Rauhamaa <marko@pacujo.net> - 2015-08-05 12:01 +0300
      Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-05 19:52 +1000
        Re: GOTCHA with list comprehension Marko Rauhamaa <marko@pacujo.net> - 2015-08-05 14:10 +0300
      Re: GOTCHA with list comprehension Saran Ahluwalia <ahlusar.ahluwalia@gmail.com> - 2015-08-07 09:08 -0400
  Re: GOTCHA with list comprehension Laura Creighton <lac@openend.se> - 2015-08-06 06:39 +0200
  Re: GOTCHA with list comprehension Chris Angelico <rosuav@gmail.com> - 2015-08-06 14:44 +1000

csiph-web