I have the following dictionary:
data = {
"id": "6018975a-dde7-4666-9436-b171c5a11dde",
"name": "Jonh Doe",
"email": "jdoe@example.org",
}
When I run my code in Python 2.7 it changes the order of the keys. I know that from Python 3.6 onward the dict type maintains the declared order. But here I am interested how to do it in Python 2.7, so please do not suggest me to switch to Python 3.
In my case the keys might be different, so I cannot use something like this because I don't know what the keys are gonna be like:
from collections import OrderedDict
my_dictionary=OrderedDict()
my_dictionary['id']="6018975a-dde7-4666-9436-b171c5a11dde"
my_dictionary['name']="Jonh Doe"
my_dictionary['email']="jdoe@example.org"
Rewriting the dict to the list of tuples did not work either:
data_list = [(k, v) for k, v in data.items()]
data_list = collections.OrderedDict(data_list)
print(data_list)
I saw the similar post here: How to keep keys/values in same order as declared? and here: Converting dict to OrderedDict
Most of the solutions were dependent on knowing the key values. As I said in my case I don't know what the keys are going to be.
Any help is appreciated! Thanks