yet another list comprehension question

R

Ross

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?
 
C

Chris Rebert

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
 
R

Ross

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.
 
C

CTO

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.
 
S

Snorri H

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)
 
A

Arnaud Delobelle

Snorri H said:
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'.
 
N

namekuseijin

2009/5/5 Ricardo Aráoz said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top