Python 3 - for x in <list> : Not iterating over entire <list> -
this question has answer here:
- strange result when removing item list [duplicate] 4 answers
- remove items list while iterating 18 answers
code:
a=[1,2,3,4,5,6,7,8,9] x in a: print(x)
output:
1 2 3 4 5 6 7 8 9
here behaviour expected , each element of list iterated over
code:
a=[1,2,3,4,5,6,7,8,9] x in a: print('x=' + str(x) if x<= 5: print('less 5') a.remove(x) print('a=' + str(a)
output:
x=1 less 5 a=[2, 3, 4, 5, 6, 7, 8, 9] x=3 less 5 a=[2, 4, 5, 6, 7, 8, 9] x=5 less 5 a=[2, 4, 6, 7, 8, 9] x=7 x=8 x=9
here behaviour unexpected , not elements of list iterated over. causing unexpected behaviour?
i'm using pre-installed python 3.5.2 on linuxmint-18.2-xfce-64bit
Comments
Post a Comment