0

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

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
anechkayf
  • 481
  • 1
  • 4
  • 12
  • 1
    You've already rejected all the ways to do it. Key-value pairs can typically *not* be relied on for ordering, and in some languages (e.g. Go) the ordering is randomised deliberately so you *won't* attempt to rely on it when it isn't guaranteed. – see sharper Aug 09 '20 at 23:13
  • 3
    The *only* way to get a Python 2.x dict to preserve key order is to use an `OrderedDict` from the start. It's impossible to recover key order from an ordinary dict, that information simply was not stored. – jasonharper Aug 09 '20 at 23:13
  • Your question doesn't really make any sense. What is the source of this dictionary? Your question implies that it is from a dict literal in the source code. If that's the case, then you need to use an ordered dict from the beginning. There is no way to recover the order from a regular dict, if there were, there *would be no need for an ordered dict to begin with*. What exactly is your use-case here? The straightforward answer to the question in your title is that it isn't possible. – juanpa.arrivillaga Aug 09 '20 at 23:48
  • @juanpa.arrivillaga this dictionary is a user's input, that's why I am not sure how can I use _OrderedDict_ since I am getting the dict from the user. – anechkayf Aug 09 '20 at 23:53
  • What does that mean *exactly*? Do you mean you are literally using the `input` function? You need to provide details or else we can only guess – juanpa.arrivillaga Aug 09 '20 at 23:53
  • @juanpa.arrivillaga I'm not using anything yet, but the plan was to use the **input** function to get the dictionary from the user. – anechkayf Aug 09 '20 at 23:55
  • 1
    @AnnaSergienko yea, don't do that. If you do it that way, then it isn't possible to recover the order. You should prompt the user to give you the information, then put that information into an `OrderedDict`. You shouldn't be using the `input` function at all, actually. But that's another issue. – juanpa.arrivillaga Aug 10 '20 at 00:01
  • @juanpa.arrivillaga Thank you for your help! – anechkayf Aug 10 '20 at 00:06

0 Answers0