473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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]]
Jun 12 '06 #1
5 1303

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

Jun 12 '06 #2
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


Jun 12 '06 #3
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)])
Jun 12 '06 #4
"Girish Sahani" <gi****@cse.iitb.ac.in> wrote in message
news:ma***************************************@pyt hon.org...
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
Jun 12 '06 #5
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>

Jun 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly...
5
by: matthias_k | last post by:
Hi, I need to sort elements of a std::list using a function predicate, something like: bool predicate( const& M m1, const& M m2 ) { return m1.somedata < m2.somedata; } I tried to call...
11
by: Tony Johansson | last post by:
Hello! I have some problem with STL function remove I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper...
9
by: Max | last post by:
I'm new with Javascript and can't seem to figure out what I'm doing wrong here as I'm not able to pass a simple variable to a function. In the head of doc I have: <script...
12
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
3
by: Girish Sahani | last post by:
Hi, I am trying to convert a list of pairs (l4) to list l5 by removing those pairs from l4 which are not present in a third list called pairList. The following is a simplified part of the routine...
5
RMWChaos
by: RMWChaos | last post by:
I am working on a script to create and remove DOM elements, and I want to make it as efficient as possible (no redundancies). Because DOM elements each have their own set of attributes, the function...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.