Function to remove elements from a list not working (corrected)

Fredrik Lundh fredrik at
Mon Jun 12 17:55:38 CEST 2006

Girish Sahani wrote:

> Btw going slightly off-topic, when i try to run a code like below with
> around 50 elements (pairs) in l4,python just hangs. Any ideas why this is
> happening...the data is not that large :((

building a filtered new list by repeatedly removing stuff from a copy
of the original list isn't exactly the fastest way to do things, but 
there's no way the following code will "hang" with 50 items instead of 
10, unless your computer is ludicrously slow (it takes about 0.000113 
seconds on my machine).

maybe you meant to write 50k items ? (11 seconds on my machine)

>> pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]]
>> l4 =
>> [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]]
>> for pair in l4[:]:
>>     if pair not in pairList:
>>         l4.remove(pair)
>> print "l4 is",l4


More information about the Python-list mailing list