Re: strange syntax rules on list comprehension conditions

Discussion in 'Python' started by Chris Mellon, Jan 18, 2008.

  1. Chris Mellon

    Chris Mellon Guest

    On Jan 18, 2008 12:53 PM, Nicholas <> wrote:
    > I was quite delighted today, after extensive searches yielded nothing, to
    > discover how to place an else condition in a list comprehension.
    > Trivial mask example:
    > >>> [True if i <5 else False for i in range(10)] # A

    > [True, True, True, True, True, False, False, False, False, False]
    >
    > I then experimented to drop the else statement which yields an error
    > >>> [i if i>3 for i in range(10)]

    > Traceback ( File "<interactive input>", line 1
    > this syntax works of course
    > >>> [i if i>3 else i for i in range(10)]

    > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >
    > Does anybody else find this lack of symmetry odd?
    >


    "x if y else x" is an expression - it's the Python equivalent of C's
    ternary operator. The mechanism for filtering in a list comp is [x for
    x in y if x].

    Your stumbling upon the ternary expression was a happy accident, and
    your confusing comes from trying to generalize the wrong operation.
     
    Chris Mellon, Jan 18, 2008
    #1
    1. Advertising

  2. Chris Mellon

    Paul McGuire Guest

    On Jan 18, 1:04 pm, "Chris Mellon" <> wrote:
    > On Jan 18, 2008 12:53 PM, Nicholas <> wrote:
    >
    > > I was quite delighted today, after extensive searches yielded nothing, to
    > > discover how to place an else condition in a list comprehension.
    > > Trivial mask example:
    > > >>> [True if i <5 else False for i in range(10)]       # A

    > > [True, True, True, True, True, False, False, False, False, False]

    >


    I think this would be preferred over your ternary-ish expression:

    >>> [ i<5 for i in range(10) ]

    [True, True, True, True, True, False, False, False, False, False]

    Do you also write code like:

    if i<5 == True:
    blah...

    If so, please just write:

    if i<5:
    better blah...

    -- Paul
     
    Paul McGuire, Jan 18, 2008
    #2
    1. Advertising

  3. Chris Mellon

    Dustan Guest

    On Jan 18, 1:04 pm, "Chris Mellon" <> wrote:
    > On Jan 18, 2008 12:53 PM, Nicholas <> wrote:
    >
    > > I was quite delighted today, after extensive searches yielded nothing, to
    > > discover how to place an else condition in a list comprehension.
    > > Trivial mask example:
    > > >>> [True if i <5 else False for i in range(10)] # A

    > > [True, True, True, True, True, False, False, False, False, False]

    >
    > > I then experimented to drop the else statement which yields an error
    > > >>> [i if i>3 for i in range(10)]


    That would be:

    [i for i in range(10) if i>3]

    > > Traceback ( File "<interactive input>", line 1
    > > this syntax works of course
    > > >>> [i if i>3 else i for i in range(10)]

    > > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
     
    Dustan, Jan 18, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Moosebumps

    List Comprehension Syntax

    Moosebumps, Jul 10, 2004, in forum: Python
    Replies:
    35
    Views:
    869
    David Eppstein
    Jul 22, 2004
  2. Replies:
    2
    Views:
    369
    Tim Peters
    Jul 12, 2004
  3. Gregory Guthrie

    list comprehension syntax..?

    Gregory Guthrie, Aug 1, 2006, in forum: Python
    Replies:
    4
    Views:
    349
    Claudio Grondi
    Aug 1, 2006
  4. Debajit Adhikary
    Replies:
    17
    Views:
    699
    Debajit Adhikary
    Oct 18, 2007
  5. Vedran Furac(
    Replies:
    4
    Views:
    339
    Marc 'BlackJack' Rintsch
    Dec 19, 2008
Loading...

Share This Page