Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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
it does not give the correct output. Please help!
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]]
 
Reply With Quote
 
 
 
 
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
> it does not give the correct output. Please help!
> 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

 
Reply With Quote
 
 
 
 
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
>> it does not give the correct output. Please help!
>> 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
>


 
Reply With Quote
 
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
> it does not give the correct output. Please help!
> 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)])
 
Reply With Quote
 
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
> it does not give the correct output. Please help!
> 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


 
Reply With Quote
 
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
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


</F>

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
Function to remove elements from a list not working Girish Sahani Python 3 06-12-2006 11:18 AM
how to remove 50000 elements from a 100000 list? Ju Hui Python 12 05-06-2006 02:44 PM
Removing elements from a list that are elements in another list Adam Hartshorne C++ 2 01-27-2006 07:47 AM
Re: how to remove duplicated elements in a list? Steve Holden Python 7 12-21-2005 12:00 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57