Python Forum
.remove() method in for-loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.remove() method in for-loop
#1
it is clear what this code do:
>>> sw = ['go', 'bo']
>>> for cm in sw:
... 	if cm == 'bo':
... 		sw.remove(cm)
... 
>>> sw 
['go'] 
but when a list contains 4 'bo' items only 2 will be removed:
>>> sw = ['go', 'bo', 'bo', 'bo', 'bo']
>>> for cm in sw:
... 	if cm == 'bo':
... 		sw.remove(cm)
... 
>>> sw 
['go', 'bo', 'bo']
>>>  
why not all 'bo' items are removed?
Reply


Messages In This Thread
.remove() method in for-loop - by nzcan - Oct-08-2018, 07:39 PM
RE: .remove() method in for-loop - by ichabod801 - Oct-08-2018, 08:25 PM
RE: .remove() method in for-loop - by TimeMen - Oct-08-2018, 08:30 PM
RE: .remove() method in for-loop - by ichabod801 - Oct-08-2018, 08:34 PM
RE: .remove() method in for-loop - by snippsat - Oct-08-2018, 10:23 PM
RE: .remove() method in for-loop - by ichabod801 - Oct-09-2018, 01:04 AM
RE: .remove() method in for-loop - by snippsat - Oct-09-2018, 02:07 PM
RE: .remove() method in for-loop - by gruntfutuk - Oct-13-2018, 07:05 PM

Forum Jump:

User Panel Messages

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