Beginner : removing tuples from a list
Anton Muhin
antonmuhin at sendmail.ru
Sat Feb 8 15:45:52 EST 2003
Fabrizio wrote:
> 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". I tried the following, but it
> doesn't work :
>
> europe = ['uk','f']
>
> x = 0
> for z in t :
> if z[2] in europe :
> t.pop(x)
> x = x +1
>
> print t
>
>
>>>>[(1, 'john', 'usa'), (3, 'jeanne', 'f'), (4, 'jack', 'usa')]
>
>
> But Jeanne is still there...
>
> TIA for your help
>
>
> Fabrizio
>
>
>
>
Hello, Fabrizio!
This should help:
notEuropean = [record for record in t if record[2] not in europe]
BTW, note , in ('2', 'jim', 'uk' *,*) --- it makes len(this tuple) = 4,
not 3 as for others.
HTH,
Anton.
More information about the Python-list
mailing list