Python 3 - for x in <list> : Not iterating over entire <list> -


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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -