473,385 Members | 1,356 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,385 software developers and data experts.

strange behaviour with remove

Hi!

Can anyone explain this to me :
$ cat test.py
l = [ 1, 2, 3 ]
d = { 'list' : l }

for x in l :
print "rm", x
d[ 'list' ].remove( x )
print "l =", l

print d

$ python test.py
rm 1
l = [2, 3]
rm 3
l = [2]
{'list': [2]}
Why 2 isn't removed ? and why l is changing during the loop ??
Am I missing something ?

My python is 2.3.4

Thanks

--
Panard
Jul 18 '05 #1
2 1273
Ok, I reply to myself, it's working when you add, line 3
l = [ i for i in l ]

I think the reason is that when you do a remove on d[ 'list' ] it will also
do a remove on l, because, I think, l in the dictionnary is only a
reference...
Panard wrote:
Hi!

Can anyone explain this to me :
$ cat test.py
l = [ 1, 2, 3 ]
d = { 'list' : l }

for x in l :
print "rm", x
d[ 'list' ].remove( x )
print "l =", l

print d

$ python test.py
rm 1
l = [2, 3]
rm 3
l = [2]
{'list': [2]}
Why 2 isn't removed ? and why l is changing during the loop ??
Am I missing something ?

My python is 2.3.4

Thanks


--
Panard
Jul 18 '05 #2
Panard wrote:
Ok, I reply to myself, it's working when you add, line 3
l = [ i for i in l ]

I think the reason is that when you do a remove on d[ 'list' ] it will also
do a remove on l, because, I think, l in the dictionnary is only a
reference...


You're partially right. The list referenced to by d['list'] is the same
object as the list referenced to by l (to verify this, do a "print
d['list'] is l"). The main reason that your loop does not work is that
it modifying a list you are iterating over leads to undefined behavior.

In your solution above, you are assigning a new list to the name "l",
iterate over this list, and remove from the original list which is now
only referenced by d['list']. This is the common solution for the
problem, although there is an easier way to copy a list:

l = l[:]

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Jul 18 '05 #3

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

Similar topics

2
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input...
8
by: Spam Trap | last post by:
I am getting strange resizing problems when using an inherited form. Controls are moving themselves seemingly randomly, but reproducibly. "frmBase" is my base class (a windows form), and...
3
by: Michael Meckelein | last post by:
Hello, I run into trouble move down a selected item in a listbox. The code moving down the item is the following one: for (int j = lv.SelectedItems.Count-1; j >=0; j--) { ListViewItem...
31
by: gamehack | last post by:
Hi all, I've been testing out a small function and surprisingly it does not work okay. Here's the full code listing: #include "stdlib.h" #include "stdio.h" char* escaped_byte_cstr_ref(char...
1
by: Asko Telinen | last post by:
Hi all. I ran into quite strange problem concerning the event raising inside FileSystemWatcher Delete event. First, i would like to describe a bit my environment. I have main GUI...
3
by: Gotch | last post by:
Hi. I found a strange behaviour I cannot explain with the ' char. I do the following: 1) there is a string with ' inside. 2) I encode the string with urlencode, I put it in a $_GET variable 3)...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
3
by: RedWiz | last post by:
Hi i have to develop a multihreaded php application on linux, then through pcntl_fork and wait. I tried it, but there something going wrong, i think. The difference whit other languages like c...
8
by: Bo Yang | last post by:
Hi, Today, I make some test on the C++ STL iterators of set containers with GNU g++ compiler. The code is: #include <set> #include <iostream> using namespace std; int main(int argc, char...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.