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

list.remove() inside for item in aList:

bartonc
6,596 Expert 4TB
I was chasing a bug last night. I actually saw it before I inserted the trace (print) statements. This:
Expand|Select|Wrap|Line Numbers
  1. >>> l=range(10)
  2. >>> for i in l:
  3. ...     l.remove(i)
  4. ...     print i
  5. ...     
0
2
4
6
8
shows interesting side effects of removing items while looping on a list's items.

However, it works working backwards in the list:
Expand|Select|Wrap|Line Numbers
  1. >>> l=range(10)
  2. >>> for i in l[::-1]:
  3. ...     l.remove(i)
  4. ...     print i
  5. ...     
9
8
7
6
5
4
3
2
1
0
Jun 28 '07 #1
4 1850
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. >>> l=range(10)
  2. >>> for i in l[:]:
  3. ...  l.remove(i)
  4. ...  print i
  5. ...
  6.  
  7. 1
  8. 2
  9. 3
  10. 4
  11. 5
  12. 6
  13. 7
  14. 8
  15. 9
  16. >>>
  17.  
  18.  
Jun 28 '07 #2
r035198x
13,262 8TB
I was chasing a bug last night. I actually saw it before I inserted the trace (print) statements. This:
Expand|Select|Wrap|Line Numbers
  1. >>> l=range(10)
  2. >>> for i in l:
  3. ...     l.remove(i)
  4. ...     print i
  5. ...     
0
2
4
6
8
shows interesting side effects of removing items while looping on a list's items.

However, it works working backwards in the list:
Expand|Select|Wrap|Line Numbers
  1. >>> l=range(10)
  2. >>> for i in l[::-1]:
  3. ...     l.remove(i)
  4. ...     print i
  5. ...     
9
8
7
6
5
4
3
2
1
0
My explanation is that in l[::-1]: a temporary shallow copy of the list is created and this is where the values of i are read from, but for
Expand|Select|Wrap|Line Numbers
  1. >>> for i in l:
The values of i are skiping one each time because after every removal, the elements' positions shift left by one.
Jun 28 '07 #3
bartonc
6,596 Expert 4TB
My explanation is that in l[::-1]: a temporary shallow copy of the list is created and this is where the values of i are read from, but for
Expand|Select|Wrap|Line Numbers
  1. >>> for i in l:
The values of i are skiping one each time because after every removal, the elements' positions shift left by one.
Exactly right! That's why ghostdog74's code works going forward. It was 4:20 AM PST when I posted that. By 9:45 that thought had occurred to me, but I was away from the computer.
Jun 28 '07 #4
r035198x
13,262 8TB
Exactly right! That's why ghostdog74's code works going forward. It was 4:20 AM PST when I posted that. By 9:45 that thought had occurred to me, but I was away from the computer.
And i'm still a Python newbie.
Wait till I can give it more time.
Jun 29 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bart Nessux | last post by:
New to Python... trying to figure out how to count the objects in a list and then map the count to the objects or convert the list to a dict... I think the latter would be better as I need a number...
3
by: GrelEns | last post by:
hello, having a list like i would like to get , , , , , ]
6
by: aurora | last post by:
I find that I use some list unpacking construct very often: name, value = s.split('=',1) So that 'a=1' unpack as name='a' and value='1' and 'a=b=c' unpack as name='a' and value='b=c'. The...
5
by: Nick L | last post by:
I've hit a brick wall on something that I'm guessing is pretty simple but it's driving me nuts. I noticed that with python lists, generally when you make a copy of a list (ie, List1 = List2) List1...
17
by: Rainer Queck | last post by:
Hi NG, one more question about thread safety of generic lists. Let's assume a generic list: List<MyTyp> aList = new List<MyType>(); Would it be a problem if one thread removes elements from...
5
by: Allerdyce.John | last post by:
In STL list, is it safe to do this: list<A> aList; //private attribute of MyClass void MyClass:: aMethod() { list<A>::Iterator iter; for ( iter = aList.begin() ; iter != aList.end() ;...
5
by: micklee74 | last post by:
hi i have a list with contents like this alist = how can i "convert" this list into a dictionary such that dictionary = { '>QWER':'askfhs' , '>REWR' : 'sfsdf' , '>FGDG', 'sdfsdgffdgfdg' }
3
by: Gregory Piñero | last post by:
I'm going to assume that it's supposed to work like this, but could someone tell me the reasoning behind it? I.E. why is 3 skipped? .... print item .... if item==2: .... alist.remove(item)...
6
by: Steven D'Aprano | last post by:
If I want to iterate over part of the list, the normal Python idiom is to do something like this: alist = range(50) # first item is special x = alist # iterate over the rest of the list for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.