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
#12
(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

(Apr-23-2017, 10:19 AM)smbx33 Wrote: I made minor modifications to the existing code and provided explanation. 

.....
while len(list_) >0: # here you had list[0] I think you wanted to stop when length reached 0
    for amount in list_: # for loop will go through the list_ of numbers one amount at a time
      .....
        list_.remove(amount) #this is to remove items from your list!

# you don't need to check the counter.  so the if statement could be deleted or reworded so if the program is run and no items are on the list it says NO ITEMS! else it prints A
Though it works - as I have pointed out in previous post, deleting an element of a list while iterating over that list is a bug, which is in this case accidentally compensated for by the external loop. In another scenario, it will not work as expected

This solves the issue
while list_:
    s += list_[0]
   .....
   list_.pop(0)
This way you always process first element of the list - while list is not empty, and you don't perform any unnecessary actions
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
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 77,488 May-15-2020, 02:27 PM
Last Post: ibreeden
  Python for loops giving error Petrus 12 5,778 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