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

G

Girish Sahani

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]]
 
W

wittempj

Girish said:
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
 
G

Girish Sahani

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 said:
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
 
B

Boris Borcic

Girish said:
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),(11,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)])
 
P

Paul McGuire

Girish Sahani said:
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
 
F

Fredrik Lundh

Girish said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top