I'm working with Enthought canopy Python 2.7.9.
A very simple Python program that involves for, range, and len:
num = 10
mylist = range(num)
for i in range(len(mylist)):
print "Size of mylist is %d" %(len(mylist))
print "i=%d" %(i)
print "mylist[%d] %d" %(i, mylist[i])
mylist=mylist[:-1]
What puzzles me is that since the length of mylist has decreased to 5, why index i can still be 5, leading to the index error?
I know in Python a for-loop works with an iterable. But I do not know how this mechanism works in this example.
for item in my_list. Check outenumerate()if you also need and index.