Beginner : removing tuples from a list

Terry Reedy tjreedy at udel.edu
Sat Feb 8 16:11:46 EST 2003


[post&cc]
"Fabrizio" <facelle at libero.it> wrote in message
news:xod1a.96483$YG2.2738784 at twister1.libero.it...
> Hi,
>
> I have a list containing tuples ("records") :
>
> t =
[(1,'john','usa'),(2,'jim','uk',),(3,'jeanne','f'),(4,'jack','usa')]
>
> I want to remove all the "european records:

recs =
[(1,'john','usa'),(2,'jim','uk',),(3,'jeanne','f'),(4,'jack','usa')]
def not_in_europe(tup): return tup[2] not in ['uk','f']

print filter(not_in_europe, recs)
>>> above gives
[(1, 'john', 'usa'), (4, 'jack', 'usa')]

Terry J. Reedy







More information about the Python-list mailing list