If we do for i in my_dict
, then it will gives the keys
, not keys: values
. Example:
>>> a = {"one" : 1, "two" : 2, "three" : 3, "four" : 4}
>>> for i in a:
... print(i)
...
four
three
two
one
So if you do for i, j in my_dict
, then it will raise an error.
You could use dict.items()
, it returns a list that saves all keys and values in tuples like this:
>>> a.items()
[('four', 4), ('three', 3), ('two', 2), ('one', 1)]
So you could do...
def getdic():
dictionary = iteratedic()
for m, n in dictionary.items():
print (m, n)
Also, dict.iteritems()
is better than dict.items()
, it returns a generator(But note that in Python 3.x, dict.iterms()
returns a generator and there's no dict.iteritems()
).
See also: What is the difference between dict.items() and dict.iteritems()?