I have a Dictionary in Python such as this: dict = {\'a\':1, \'q\':1, \'l\':2, \'m\':1, \'u\':1, \'i\':1}
Is there any way that I can keep the order of this diction
If the dictionary you want to order is created outside of your control, you might use the following to get its key:value
pairs as a list of tuples:
pairs = my_dict.items()
You can then sort this list any way you like. When you've done that, you can pass the ordered list of pairs to the OrderedDict
constructor
from collections import OrderedDict
# sort in some way (for example, reverse the key order)
pairs = reversed(my_dict.items())
ordered = OrderedDict(pairs)