I have a dictionary
{'a': 'first', 'b': 'second'}
However, I need the dictionary in a different order:
{'b': 'second', 'a': 'first'}
What is the best way to do this?
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.
Dictionaries are not ordered. So there is no way to do it.
If you have python2.7+, you can use
collections.OrderedDict– in this case you could retrieve the item list using.items()and then reverse it and create a newOrderedDictfrom the reversed list:If you are using an older python version you can get a backport from http://code.activestate.com/recipes/576693/