I am working with Google Python class exercises where I am getting this issue –
def front_x(words):
# +++your code here+++
list = []
for i,s in enumerate(words):
print i,s
if s[0] == 'x':
list.append(words.pop(i))
return list
print front_x(['bbb','ccc','axx','xzz','xaa'])
my loop is only iterating from 0 to 3, so print i,s is giving me values till ‘xzz’.Please point where I am wrong.
Don’t modify something as you’re iterating over it.
words.pop(i)modifieswords, which you’re iterating over viaenumerate().I’d suggest looking at list comprehensions for accomplishing your apparent goal.