When I try and sort my dictionary, I get an error: ''nonetype' object is not iterable.
I am doing:
for k,v in mydict.items().sort():
When I try and sort my dictionary, I get an error: ''nonetype' object is not iterable.
I am doing:
for k,v in mydict.items().sort():
The sort
method returns None
(it has sorted the temporary list given by items()
, but that's gone now). Use:
for k, v in sorted(mydict.iteritems()):
Using .items()
in lieu of .iteritems()
is also OK (and needed if you're in Python 3) but, in Python 2 (where .items()
makes and returns a list while .iteritems()
doesn't, just returns an iterator), avoiding the making of an extra list is advantageous -- sorted
will make its own list to return, anyway, without altering the argument passed to it.