list - Python for-loop deletion stops halfway -
just started learning python today. i'm enjoying , docs helpful people say. there's i'm not getting 2 of python's idiosyncrasies: lists , for-loops.
the result of code:
lets = ['a','b','c','d','e','f','g','h','i','j'] j in lets: del lets[0] print(lets) is this:
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] ['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] ['d', 'e', 'f', 'g', 'h', 'i', 'j'] ['e', 'f', 'g', 'h', 'i', 'j'] ['f', 'g', 'h', 'i', 'j'] why stop @ 5 steps instead of ten (and hence delete items)?
from can tell, have ten elements in lets. loop stops @ five. after deleting 5 elements have 5 elements left. after fifth loop, checks how many elements in lets, sees five, , stops has looped 5 times.
Comments
Post a Comment