Python Forum
Error in loops, new to Python
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in loops, new to Python
#17
(Apr-24-2017, 06:41 PM)volcano63 Wrote:
(Apr-24-2017, 03:43 AM)nilamo Wrote: Modifying a list while you're in the middle of iterating over that same list is very dangerous.
Usually - from the last time I SNAFUed this way (several years ago  Blush) - iterator skips element on each deletion. It's not dangerous - it's a bug
For me it looks like appropriate behaviour - from my understanding list iterator (created with for clause) only keeps information about list object and current index. For i-th loop it gives i-th item from the actual list - and if in i-th loop i-th item is different than i-th item before start of loop, well, its not for/iterator problem, but yours. So if you are removing items from the start of the list  while looping over it, it looks like it skips every second item, while if you are removing items from the end of the list, it looks like iteration over first half of the list only. Indeed it makes things rather confusing , so its bettter to avoid it...

(Apr-24-2017, 06:47 PM)nilamo Wrote: I think the easy solution is to just iterate over a copy of the list, ie: for x in some_list[:]:, but then if the list is large (or is a generator and doesn't exist fully yet), that could just eat memory.  Maybe for x in (item for item in some_list):?  That way you can modify the original list, without changing how you iterate.  Could make for confusing results sometimes, though.
Such generator expression would not work, it references original list too, changes would "propagate".
Reply


Messages In This Thread
Error in loops, new to Python - by jhall710 - Apr-17-2017, 10:42 PM
RE: Error in loops, new to Python - by nilamo - Apr-18-2017, 02:18 AM
RE: Error in loops, new to Python - by jhall710 - Apr-18-2017, 02:27 AM
RE: Error in loops, new to Python - by nilamo - Apr-18-2017, 02:28 AM
RE: Error in loops, new to Python - by jhall710 - Apr-18-2017, 02:35 AM
RE: Error in loops, new to Python - by nilamo - Apr-18-2017, 03:29 AM
RE: Error in loops, new to Python - by sparkz_alot - Apr-18-2017, 12:50 PM
RE: Error in loops, new to Python - by smbx33 - Apr-23-2017, 10:19 AM
RE: Error in loops, new to Python - by nilamo - Apr-24-2017, 03:43 AM
RE: Error in loops, new to Python - by smbx33 - Apr-24-2017, 04:32 AM
RE: Error in loops, new to Python - by volcano63 - Apr-24-2017, 06:41 PM
RE: Error in loops, new to Python - by nilamo - Apr-24-2017, 04:05 PM
RE: Error in loops, new to Python - by wavic - Apr-24-2017, 06:47 PM
RE: Error in loops, new to Python - by nilamo - Apr-24-2017, 06:47 PM
RE: Error in loops, new to Python - by wavic - Apr-24-2017, 06:53 PM
RE: Error in loops, new to Python - by volcano63 - Apr-24-2017, 07:02 PM
RE: Error in loops, new to Python - by zivoni - Apr-24-2017, 07:50 PM
RE: Error in loops, new to Python - by volcano63 - Apr-24-2017, 08:16 PM
RE: Error in loops, new to Python - by smbx33 - Apr-24-2017, 08:49 PM
RE: Error in loops, new to Python - by nilamo - Apr-24-2017, 09:44 PM
RE: Error in loops, new to Python - by smbx33 - Apr-25-2017, 05:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  New to python! Loops Seeley307 3 68,654 May-15-2020, 02:27 PM
Last Post: ibreeden
  Python for loops giving error Petrus 12 5,656 Jan-09-2019, 08:02 AM
Last Post: Petrus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020