473,387 Members | 1,453 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.

list traversal and remove

I supposed the below code will print seven 2 and generate the list li
without 2.
Strangely it only print four 2. If you change the number of 2 in the
list, the results are all beyond expectation.
I know the other way to achieve the expected goal, but why this is
happening? Could somebody enlight me?

li= [2,2,2,2,2,2,2,3,4,5,6,7,8,9]
for x in li:
if x == 2:
print x
li.remove(x)
Jan 31 '08 #1
3 1333
On Jan 31, 7:49*am, "digisat...@gmail.com" <digisat...@gmail.com>
wrote:
I supposed the below code will print seven 2 and generate the list li
without 2.
Strangely it only print *four 2. If you change the number of 2 in the
list, the results are all beyond expectation.
I know the other way to achieve the expected goal, but why this is
happening? Could somebody enlight me?

li= [2,2,2,2,2,2,2,3,4,5,6,7,8,9]
for x in li:
* * if x == 2:
* * * * print x
* * * * li.remove(x)
There's just been a thread on this:

http://groups.google.com/group/comp....623baaa46e72a/

--
Arnaud
Jan 31 '08 #2
On Wed, 30 Jan 2008 23:49:46 -0800, di********@gmail.com wrote:
I supposed the below code will print seven 2 and generate the list li
without 2.
Strangely it only print four 2. If you change the number of 2 in the
list, the results are all beyond expectation. I know the other way to
achieve the expected goal, but why this is happening? Could somebody
enlight me?
Do not modify a list at the same time that you are traversing it.

If you look at the archives (say, on Google Groups, or any number of
other places), this topic was just discussed yesterday and earlier today.

See the thread with subject line:

"Removal of element from list while traversing causes the next element
to be skipped"

As for why it is happening... you have code that looks like this:

for x in li:
if x == 2:
print x
li.remove(x)
Consider that "under the hood", the for-loop looks something like this:

i = 0
while i < the length of the list:
set x equal to the item in the i-th position
execute the loop block of code
i += 1

If you start deleting or inserting items in the middle of the loop, the
index won't be pointing where you expect.

--
Steven
Jan 31 '08 #3
Thank you very much for the great anwsers. You guys save my sleep
today.
Jan 31 '08 #4

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

Similar topics

11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
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...
4
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows...
4
by: eksamor | last post by:
I have a simple linked list: struct element { struct element *next; int start; }; struct list { struct element *head;
19
by: jason_box | last post by:
I'm alittle new at C and I'm trying to write a simple program that will record the frequency of words and just print it out. It is suppose to take stdin and I heard it's only a few lines but I'm...
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:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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,...

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.