I am confused how python is iterating through this dictionary. From python’s documentation, the itervalues returns an iterator over the dictionary’s values.
dict = {"hello" : "wonderful", "today is" : "sunny", "more text" : "is always good"}
for x in dict.itervalues():
x = x[2:]
print dict
This prints out the original dictionary unchanged. Why is that? If I am saying the value at position x is “blabla”, why is it not getting set?
This has nothing to do with strings or lists. The devil is in how the
foris unfolded.Doing
is more-or-less equivalent to doing
So with this in mind it’s not very hard to see that we are just reassigning
x, which holds a result from a function call.