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():
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
sortmethod returnsNone(it has sorted the temporary list given byitems(), but that’s gone now). Use: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 —sortedwill make its own list to return, anyway, without altering the argument passed to it.