29

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?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Qiao
  • 16,565
  • 29
  • 90
  • 117
  • Ok, my fault, I'll read documentation. I just print it and it was out by specific order by chance. I thought it has something to do with a way it is written. Like php's array. – Qiao May 21 '11 at 18:19
  • @Qiao This is actually a good question. I was like "oh, that'll be easy to answer with the documentation" -- it isn't. This "trivial fact" seems to be assumed knowledge in most places. The best I can find (in OrderedDict, chapter 8.3) is "An OrderedDict is a dict that remembers the order that keys were first inserted.". –  May 21 '11 at 18:22
  • 1
    Ahh, there it is: "It is best to think of a dictionary as an *unordered* set of key: value pairs, with the requirement that the keys are unique (within one dictionary)." from [5.5 Data Structures in the Tutorial](http://docs.python.org/tutorial/datastructures.html#dictionaries). –  May 21 '11 at 18:29
  • 3
    -1. Searching StackOverflow for "python dictionary order" easily finds the answer. You should at least try *something* before posting to StackOverflow. – Steven Rumbalski May 21 '11 at 18:43
  • 1
    possible duplicate of [In what order does python display dictionary keys?](http://stackoverflow.com/questions/4458169/in-what-order-does-python-display-dictionary-keys) – dawg May 21 '11 at 19:59
  • @pst: Also [Library Reference, Ch. 5 "Built-in Types"](http://docs.python.org/library/stdtypes.html#dict.items): CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. – jscs May 22 '11 at 05:58
  • @Steven Rumbalski, I was looking for "how to change dictionary order?", not for "is it possible to change dictionary order?". They are different questions, though have the same answer. – Qiao May 22 '11 at 09:46
  • @Qiao: If they have the same answer, then they're dupes. The idea is to get all the answers in one place. – jscs May 23 '11 at 17:28
  • possible duplicate of [How to reverse order of keys in python dict?](http://stackoverflow.com/questions/5455606/how-to-reverse-order-of-keys-in-python-dict) – om-nom-nom Sep 16 '13 at 14:34

3 Answers3

41

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 new OrderedDict from the reversed list:

>>> od = OrderedDict((('a', 'first'), ('b', 'second')))
>>> od
OrderedDict([('a', 'first'), ('b', 'second')])
>>> items = od.items()  # list(od.items()) in Python3
>>> items.reverse()
>>> OrderedDict(items)
OrderedDict([('b', 'second'), ('a', 'first')])

If you are using an older python version you can get a backport from http://code.activestate.com/recipes/576693/

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
8

Dictionaries don't have order.

You can get the keys, order them however you like, then iterate the dictionary values that way.

keys = myDict.keys()
keys = sorted(keys)  # order them in some way
for k in keys:
   v = myDict[k]
eduffy
  • 39,140
  • 13
  • 95
  • 92
4

You can't; dicts are unsortable. Use an OrderedDict if you need an ordered dictionary.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • `dict` is sortable (you can always apply `sorted` to it) but the default order of keys cannot be influenced. – sophros Jan 25 '19 at 08:15
  • what if the keys are numeric & inserted incrementally `eg:1,2,3,....10`. So in this case, the order is preserved right? – Sunni Aug 23 '19 at 13:35