-2

I have a dictionary with a specific order of keys(the values don't matter). I have another dictionary with the same keys, but not in the same order:

dict_1 = {"name" : "Joe", "class" : "8", "marks" : "80"}
dict_2 = {"class" : "9", "marks" : "90", "name" : "Adam"}

Now after sorting dict_2, I want it to be as {"name" : "Adam", "class" : "9", "marks" : "90"}. I know that this is possible since dictionaries are ordered by default in Python 3, but I couldn't get any solution even after a lot of research.

Edit: Another question is similar to this, and the answers given there could be used if the list to be based off is defined, in this case the keys of another dictionary(dict_1). Answers here are more effecient

  • Does this answer your question? [How to sort a dictionary based on a list in python](https://stackoverflow.com/questions/21773866/how-to-sort-a-dictionary-based-on-a-list-in-python) – Maurice Meyer Sep 07 '22 at 17:26
  • 1
    Dictionaries still aren't semantically ordered data structures (for example, equality comparison ignores the order). What's the _actual_ problem you're trying to solve? – jonrsharpe Sep 07 '22 at 17:26
  • Does this answer your question? [Custom Sorting Python Dictionary](https://stackoverflow.com/questions/12031482/custom-sorting-python-dictionary) – Clark Ngo Sep 07 '22 at 17:27

3 Answers3

3

Keys are sorted in insertion order. That means that you need to either remove the keys from dict_2 and add them back in the right order, or just make a new dictionary.

You can get the same keys as dict_1 by copying the entire dictionary and reassigning the values:

result = dict_1.copy()
for key in result:
    result[key] = dict_2[key]

You can shorten the loop to

result.update(dict_2)

A similar result would be obtained from merging the dictionaries in order, with dict_1 used to establish the order of keys, and dict_2 to supply the values:

{**dict_1, **dict_2}

As of Python 3.9, PEP-0584 introduces the union operator for dictionaries, so you can write the same thing as

dict_1 | dict_2

You can also just iterate over the keys in dict_1:

result = {k: dict_2[k] for k in dict_1}

Or you can pop-and-add if you want to keep the same object for some reason:

for key in dict_1:
    dict_2[key] = dict_2.pop(key)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
2

What are you actually trying to accomplish by having the dictionary keys in the same order? Presumably you want to iterate over them in some order, say to print them. Instead of rearranging every dictionary, why not simply have a list of the keys in the order you want them, and iterate over that list to get the values for that key in that order?

keys = ["class", "marks", "name"]
for key in keys:
    print(key, dict1[key])

If you really do want to rearrange the dictionaries, the most straightforward way (IMHO) is to create a new dictionary in the desired order.

keys = ["class", "marks", "name"]    
dict1 = {key: dict1[key] for key in keys}

Write a function for doing this so you don't need to repeat yourself.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • You can rearrange in-place too – Mad Physicist Sep 07 '22 at 17:30
  • good point, reworded – kindall Sep 07 '22 at 17:31
  • I just had to compare a dictionary that was inputted randomly with numerous other dictionaries with the same keys and specific order. Using the "update" method seems to be the shortest so I will use that. Thank you for the answer though – Absolute Reality Sep 07 '22 at 18:48
  • ... comparing dictionaries **does not require them to be in the same order.** As hinted by my link, you should have asked about what you are actually trying to do, rather than asking about how to implement an imagined solution. – kindall Sep 07 '22 at 18:53
  • I meant comparing dictionaries as in by using for loops, and I needed the keys to be in the same order to "compare". I even had a counter of how many values are the same in the dictionaries. Is there any better way of comparing? – Absolute Reality Sep 07 '22 at 19:03
  • 1
    Why do you need the keys to be in the same order to compare the dictionaries? Iterate over one dictionary (getting its keys one at a time) then get the same item from the second dictionary using each key. – kindall Sep 07 '22 at 20:51
1

This can be done by building a new dictionary using the keys from dict_1.

dict_1 = {"name" : "Joe", "class" : "8", "marks" : "80"}
dict_2 = {"class" : "9", "marks" : "90", "name" : "Adam"}

key_list = list(dict_1)

dict_3 = {}
for key in key_list:
    dict_3[key] = dict_2[key]

dict_3 now contains the values from dict_2, but using the key order of dict_1. Of course, you could also use this method to build dict_2 to begin with.

John
  • 11
  • 3