yet another list comprehension question

Discussion in 'Python' started by Ross, May 3, 2009.

  1. Ross

    Ross Guest

    I'm trying to set up a simple filter using a list comprehension. If I
    have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
    and I wanted to filter out all tuples containing None, I would like to
    get the new list b = [(1,2), (3,4),(6,7)].

    I tried b = [i for i in a if t for t in i is not None] but I get the
    error that "t is not defined". What am I doing wrong?
    Ross, May 3, 2009
    #1
    1. Advertising

  2. Ross

    Chris Rebert Guest

    On Sat, May 2, 2009 at 7:13 PM, Ross <> wrote:
    > I'm trying to set up a simple filter using a list comprehension. If I
    > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
    > and I wanted to filter out all tuples containing None, I would like to
    > get the new list b = [(1,2), (3,4),(6,7)].


    b = [tup for tup in a if None not in tup]

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, May 3, 2009
    #2
    1. Advertising

  3. Ross

    Ross Guest

    On May 2, 7:21 pm, Chris Rebert <> wrote:
    > On Sat, May 2, 2009 at 7:13 PM, Ross <> wrote:
    > > I'm trying to set up a simple filter using a list comprehension. If I
    > > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
    > > and I wanted to filter out all tuples containing None, I would like to
    > > get the new list b = [(1,2), (3,4),(6,7)].

    >
    > b = [tup for tup in a if None not in tup]
    >
    > Cheers,
    > Chris
    > --http://blog.rebertia.com


    Thanks I feel retarded sometimes.
    Ross, May 3, 2009
    #3
  4. Ross

    CTO Guest

    On May 2, 10:13 pm, Ross <> wrote:
    > I'm trying to set up a simple filter using a list comprehension. If I
    > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
    > and I wanted to filter out all tuples containing None, I would like to
    > get the new list b = [(1,2), (3,4),(6,7)].


    try this:

    b = [i for i in a if None not in i]

    > I tried b = [i for i in a if t for t in i is not None]   but I get the
    > error that "t is not defined". What am I doing wrong?


    You've got a "for" and an "if" backwards. t isn't defined when the if
    tries to evaluate it.
    CTO, May 3, 2009
    #4
  5. Ross

    Snorri H Guest

    On May 3, 6:13 am, Ross <> wrote:
    > I'm trying to set up a simple filter using a list comprehension. If I
    > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
    > and I wanted to filter out all tuples containing None, I would like to
    > get the new list b = [(1,2), (3,4),(6,7)].
    >
    > I tried b = [i for i in a if t for t in i is not None] but I get the
    > error that "t is not defined". What am I doing wrong?



    Works as well:
    filter(lambda x:not None in x, your_list)
    Snorri H, May 4, 2009
    #5
  6. Snorri H <> writes:

    > On May 3, 6:13 am, Ross <> wrote:
    >> I'm trying to set up a simple filter using a list comprehension. If I
    >> have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
    >> and I wanted to filter out all tuples containing None, I would like to
    >> get the new list b = [(1,2), (3,4),(6,7)].
    >>
    >> I tried b = [i for i in a if t for t in i is not None] but I get the
    >> error that "t is not defined". What am I doing wrong?

    >
    >
    > Works as well:
    > filter(lambda x:not None in x, your_list)

    ^^^^^^^^^^^^^
    This is usually spelt 'None not in x'.

    --
    Arnaud
    Arnaud Delobelle, May 4, 2009
    #6
  7. On Mon, May 4, 2009 at 2:33 AM, namekuseijin
    <> wrote:
    >>>> ls = [(1,2), (3,4), (5, None), (6,7), (8, None)]
    >>>> [(x,y) for (x,y) in ls if y]

    > [(1, 2), (3, 4), (6, 7)]


    Nope. That filters out 0 as well as None. Not what the OP asked for.
    David Robinow, May 4, 2009
    #7
  8. Ross

    namekuseijin Guest

    On May 4, 9:15 am, David Robinow <> wrote:
    > On Mon, May 4, 2009 at 2:33 AM, namekuseijin
    >
    > <> wrote:
    > >>>> ls = [(1,2), (3,4), (5, None), (6,7), (8, None)]
    > >>>> [(x,y) for (x,y) in ls if y]

    > > [(1, 2), (3, 4), (6, 7)]

    >
    > Nope. That filters out 0 as well as None. Not what the OP asked for.


    True. I'm still a C programmer at heart I guess. ah, the flexibility
    of 0... ;)
    namekuseijin, May 4, 2009
    #8
  9. Ross

    namekuseijin Guest

    2009/5/5 Ricardo Aráoz <>:
    > This seems to work for any length tuples :
    >
    >>>> a = [(1,2), (3,4, 'goes'), (5,None), (6,7, 8, 'as', None), (8, None),
    >>>> (9, 0)]
    >>>> [tup for tup in a if not [e for e in tup if e == None]]

    > [(1, 2), (3, 4, 'goes'), (9, 0)]


    Why that extra "for"? KISS

    >>> a = [(1,2), (3,4, 'goes'), (5,None), (6,7, 8, 'as', None), (8, None), (9, 0)]

    [(1, 2), (3, 4, 'goes'), (5, None), (6, 7, 8, 'as', None), (8, None), (9, 0)]
    >>> [t for t in a if None not in t]

    [(1, 2), (3, 4, 'goes'), (9, 0)]

    "in" works perfectly well for any sequence, including strings.
    namekuseijin, May 6, 2009
    #9
    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. palo
    Replies:
    4
    Views:
    238
    Christoph Zwerschke
    Jan 22, 2006
  2. Berehem
    Replies:
    4
    Views:
    549
    Lawrence Kirby
    Apr 28, 2005
  3. Shane Geiger
    Replies:
    4
    Views:
    381
    bullockbefriending bard
    Mar 25, 2007
  4. Debajit Adhikary
    Replies:
    17
    Views:
    678
    Debajit Adhikary
    Oct 18, 2007
  5. Vedran Furac(
    Replies:
    4
    Views:
    321
    Marc 'BlackJack' Rintsch
    Dec 19, 2008
Loading...

Share This Page