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

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

Girish Sahani
Guest
Posts: n/a

 06-12-2006
Hi,
I am trying to modify a list of pairs (l4) by removing those
pairs which are not present in a third list called pairList.
The following is a simplified part of the routine i have written. However
Its possible i have made a trivial mistke since i am a newbie.

def getl5():
l5 = []
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

The output given is:
l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]]

wittempj@hotmail.com
Guest
Posts: n/a

 06-12-2006

Girish Sahani wrote:
> Hi,
> I am trying to modify a list of pairs (l4) by removing those
> pairs which are not present in a third list called pairList.
> The following is a simplified part of the routine i have written. However
> Its possible i have made a trivial mistke since i am a newbie.
>
> def getl5():
> l5 = []
> 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
>
> The output given is:
> l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]]

It is better to iterate over a copy, e.g. like this:

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

Girish Sahani
Guest
Posts: n/a

 06-12-2006
Thank you Mark....this works too...
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 (
>
> Girish Sahani wrote:
>> Hi,
>> I am trying to modify a list of pairs (l4) by removing those
>> pairs which are not present in a third list called pairList.
>> The following is a simplified part of the routine i have written.
>> However
>> Its possible i have made a trivial mistke since i am a newbie.
>>
>> def getl5():
>> l5 = []
>> 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
>>
>> The output given is:
>> l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]]

>
> It is better to iterate over a copy, e.g. like this:
>
> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Boris Borcic
Guest
Posts: n/a

 06-12-2006
Girish Sahani wrote:
> Hi,
> I am trying to modify a list of pairs (l4) by removing those
> pairs which are not present in a third list called pairList.
> The following is a simplified part of the routine i have written. However
> Its possible i have made a trivial mistke since i am a newbie.
>
> def getl5():
> l5 = []
> 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
>
> The output given is:
> l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]]

use sets

def gets5() :
pairSet = set([(1,2),(3,4),(3,5),(3,6),(9,7),(8,9),(8,7),(7,9),(1 1,10)])
s4= set([(4,2),(4,7),(4,10),(4,12),(9,2),(9,7),(9,10),(9,12 ),(11,2),(11,7)])
s4 &= pairSet
print "s4 is",s4

the output is

s4 is set([(9, 7)])

Paul McGuire
Guest
Posts: n/a

 06-12-2006
"Girish Sahani" <> wrote in message
news:mailman.6916.1150100741.27775.python-...
> Hi,
> I am trying to modify a list of pairs (l4) by removing those
> pairs which are not present in a third list called pairList.
> The following is a simplified part of the routine i have written. However
> Its possible i have made a trivial mistke since i am a newbie.
>

You've fallen victim to one of the Classic Blunders! The First is "Never
start a land war in Asia!", but the second, only slightly lesser known is
"Never modify a list that you are iterating over!"

-- Paul

Fredrik Lundh
Guest
Posts: n/a

 06-12-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
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

</F>

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Debajit Adhikary Python 17 10-18-2007 06:45 PM Girish Sahani Python 3 06-12-2006 11:18 AM Ju Hui Python 12 05-06-2006 02:44 PM Adam Hartshorne C++ 2 01-27-2006 07:47 AM Steve Holden Python 7 12-21-2005 12:00 AM