How to keep keys/values in same order as declared?
Asked Answered
R

13

403

I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.

So if I have the dictionary:

d = {'ac': 33, 'gw': 20, 'ap': 102, 'za': 321, 'bs': 10}

It isn't in that order if I view it or iterate through it. Is there any way to make sure Python will keep the explicit order that I declared the keys/values in?

Raffle answered 8/12, 2009 at 15:53 Comment(1)
Use python 3.7 or later will help you having ordering preservedVelocity
P
380

From Python 3.6 onwards, the standard dict type maintains insertion order by default.

Defining

d = {'ac':33, 'gw':20, 'ap':102, 'za':321, 'bs':10}

will result in a dictionary with the keys in the order listed in the source code.

This was achieved by using a simple array with integers for the sparse hash table, where those integers index into another array that stores the key-value pairs (plus the calculated hash). That latter array just happens to store the items in insertion order, and the whole combination actually uses less memory than the implementation used in Python 3.5 and before. See the original idea post by Raymond Hettinger for details.

In 3.6 this was still considered an implementation detail; see the What's New in Python 3.6 documentation:

The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations; this also helps preserve backwards-compatibility with older versions of the language where random iteration order is still in effect, e.g. Python 3.5).

Python 3.7 elevates this implementation detail to a language specification, so it is now mandatory that dict preserves order in all Python implementations compatible with that version or newer. See the pronouncement by the BDFL. As of Python 3.8, dictionaries also support iteration in reverse.

You may still want to use the collections.OrderedDict() class in certain cases, as it offers some additional functionality on top of the standard dict type. Such as as being reversible (this extends to the view objects), and supporting reordering (via the move_to_end() method).

Physiography answered 16/9, 2016 at 17:40 Comment(4)
is this langage specification feature available in the official documentation somewhere ? (was not able to find it)Longe
@ManuelSelva the 3.7 what’s new documents mention it (linking to the email I quoted). The dictionary view objects section further documents iteration order (under iter(dictview): Keys and values are iterated over in insertion order. and Dictionary order is guaranteed to be insertion order.).Physiography
@ManuelSelva the standard type hierarchy section of the data model docs also cover the subject (Dictionaries preserve insertion order, meaning that keys will be produced in the same order they were added sequentially over the dictionary.).Physiography
@ManuelSelva the convention is: unless explicitly stated something is a CPython implementation detail, the description of a standard type feature in the docs makes that feature part of the language spec.Physiography
A
182
from collections import OrderedDict
OrderedDict((word, True) for word in words)

contains

OrderedDict([('He', True), ('will', True), ('be', True), ('the', True), ('winner', True)])

If the values are True (or any other immutable object), you can also use:

OrderedDict.fromkeys(words, True)
Apoplexy answered 11/6, 2012 at 14:26 Comment(7)
Worth noting, of course, that the 'immutable' part isn't a hard and fast rule that Python will enforce - its "only" a good idea.Depressor
be aware that solutions like: OrderedDict(FUTURE=[], TODAY=[], PAST=[]) wont't work, when mentioned aproach: OrderedDict([('FUTURE', []), ('TODAY', []), ('PAST', [])]) will keep order.Untitled
@andi I got another problem,when using jsonify, the OrderedDict seems lost it's order when generate the json data.Anyway to solve this?Appendicle
github.com/pallets/flask/issues/974 this can be used to solve the problem..Appendicle
Python3.7 now has dict ordered by default. mail.python.org/pipermail/python-dev/2017-December/151283.htmlCinerary
As this is the highest ranking answer on this thread I would suggest editing in the fact that since Python 3.7 dictionaries are guarenteed to keep the insertion orderMona
The fromKeys method helped me make a fast "OrderedSet" to remove duplicates: list(OrderedDict.fromkeys(list_with_dups, True).keys())Labyrinth
D
177

Rather than explaining the theoretical part, I'll give a simple example.

>>> from collections import OrderedDict
>>> my_dictionary=OrderedDict()
>>> my_dictionary['foo']=3
>>> my_dictionary['aol']=1
>>> my_dictionary
OrderedDict([('foo', 3), ('aol', 1)])
>>> dict(my_dictionary)
{'foo': 3, 'aol': 1}
Dustcloth answered 30/1, 2015 at 7:29 Comment(6)
Is there a way to mass assign OrderedDict like the Dict type?Appendicle
OrderedDict indeed solves the problem, but... in this particular example you get exactly the same result using a standard dictionaryCloud
@Tonechas: I just tried the example with a standard dictionary, and got {'aol': 1, 'foo': 3} So I think it's a good illustrative example.Aerometry
There's a lesson for everyone: it was discovered (I think around the 2.4 release) that Python's predictable hashing might give rise to security vulnerabilities, so now there's no guarantee that even two different runs of the same code will give the same ordering in a standard dict.Litigant
Why can't we pass some format of the order we want to mass initialize with our values? Instead of assigning in each line one value? (python2.7)Kussell
@tyan you can call OrderedDict.update() with an iterable containing key-value pairs: d1.upate([(key1, val1), (key2, val2)]).Horned
L
42

Note that this answer applies to python versions prior to python3.7. CPython 3.6 maintains insertion order under most circumstances as an implementation detail. Starting from Python3.7 onward, it has been declared that implementations MUST maintain insertion order to be compliant.


python dictionaries are unordered. If you want an ordered dictionary, try collections.OrderedDict.

Note that OrderedDict was introduced into the standard library in python 2.7. If you have an older version of python, you can find recipes for ordered dictionaries on ActiveState.

Landside answered 11/6, 2012 at 14:25 Comment(1)
see @martijn's post above. From python 3.6 onwards, dict supports insertion ordering.Detect
F
12

Dictionaries will use an order that makes searching efficient, and you cant change that,

You could just use a list of objects (a 2 element tuple in a simple case, or even a class), and append items to the end. You can then use linear search to find items in it.

Alternatively you could create or use a different data structure created with the intention of maintaining order.

Frederickafredericks answered 8/12, 2009 at 15:57 Comment(1)
Dictionaries will use an order that makes searching efficient Finally, someone pointed it out.Algolagnia
T
7

I came across this post while trying to figure out how to get OrderedDict to work. PyDev for Eclipse couldn't find OrderedDict at all, so I ended up deciding to make a tuple of my dictionary's key values as I would like them to be ordered. When I needed to output my list, I just iterated through the tuple's values and plugged the iterated 'key' from the tuple into the dictionary to retrieve my values in the order I needed them.

example:

test_dict = dict( val1 = "hi", val2 = "bye", val3 = "huh?", val4 = "what....")
test_tuple = ( 'val1', 'val2', 'val3', 'val4')
for key in test_tuple: print(test_dict[key])

It's a tad cumbersome, but I'm pressed for time and it's the workaround I came up with.

note: the list of lists approach that somebody else suggested does not really make sense to me, because lists are ordered and indexed (and are also a different structure than dictionaries).

Thirtyeight answered 16/6, 2014 at 21:17 Comment(1)
Great solution. I will use it to write json to file, always in the same order.Daddy
C
7

You can't really do what you want with a dictionary. You already have the dictionary d = {'ac':33, 'gw':20, 'ap':102, 'za':321, 'bs':10}created. I found there was no way to keep in order once it is already created. What I did was make a json file instead with the object:

{"ac":33,"gw":20,"ap":102,"za":321,"bs":10}

I used:

r = json.load(open('file.json'), object_pairs_hook=OrderedDict)

then used:

print json.dumps(r)

to verify.

Chondrule answered 2/9, 2016 at 1:5 Comment(2)
So why not start with an OrderedDict from a list? The JSON file doesn't really add anything here.Physiography
Yes, list is more useful to keep order but the answer was in regards to the question about ordering dictionaries. Just letting people know about the limitations of using a dictionary and giving them a possible work around if they need to use a dictionary for some reason.Chondrule
F
5
from collections import OrderedDict
list1 = ['k1', 'k2']
list2 = ['v1', 'v2']
new_ordered_dict = OrderedDict(zip(list1, list2))
print new_ordered_dict
# OrderedDict([('k1', 'v1'), ('k2', 'v2')])
Fantastically answered 15/5, 2019 at 7:36 Comment(1)
main problem that isn't a dict any more, it's a list of tuplesCongratulant
F
2

I had a similar problem when developing a Django project. I couldn't use OrderedDict, because I was running an old version of python, so the solution was to use Django's SortedDict class:

https://code.djangoproject.com/wiki/SortedDict

e.g.,

from django.utils.datastructures import SortedDict
d2 = SortedDict()
d2['b'] = 1
d2['a'] = 2
d2['c'] = 3

Note: This answer is originally from 2011. If you have access to Python version 2.7 or higher, then you should have access to the now standard collections.OrderedDict, of which many examples have been provided by others in this thread.

Foxhole answered 2/6, 2011 at 15:46 Comment(0)
P
2

Another alternative is to use Pandas dataframe as it guarantees the order and the index locations of the items in a dict-like structure.

Petrography answered 18/3, 2019 at 1:43 Comment(0)
K
1

Generally, you can design a class that behaves like a dictionary, mainly be implementing the methods __contains__, __getitem__, __delitem__, __setitem__ and some more. That class can have any behaviour you like, for example prividing a sorted iterator over the keys ...

Knit answered 8/12, 2009 at 16:9 Comment(0)
S
1

if you would like to have a dictionary in a specific order, you can also create a list of lists, where the first item will be the key, and the second item will be the value and will look like this example

>>> list =[[1,2],[2,3]]
>>> for i in list:
...     print i[0]
...     print i[1]

1
2
2
3
Seppala answered 4/7, 2013 at 20:47 Comment(2)
That is not a "dictionary" because you cannot lookup items by their key without searching through the entire collection (taking O(n) time).Tabby
Yes, it is not a dictionary, but, depending on the situation, it could provide a valid solution the problem of the original poster.Rodmur
M
0

You can do the same thing which i did for dictionary.

Create a list and empty dictionary:

dictionary_items = {}
fields = [['Name', 'Himanshu Kanojiya'], ['email id', '[email protected]']]
l = fields[0][0]
m = fields[0][1]
n = fields[1][0]
q = fields[1][1]
dictionary_items[l] = m
dictionary_items[n] = q
print dictionary_items
Magic answered 29/1, 2019 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.