473,325 Members | 2,771 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,325 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 1852
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.