0

Based on what I know, you can't assume that a data structure (such as dictionary) will save the values in it on the same order as what you have initialized it. For example:

d = {1:10,2:20,3:30}

when you print it inside a for loop, the result could be:

{2:20,1:10,3:30}

Why it happens - why the dictionary (or the other data structure) won't keep the values in a specific order? Is it true only for dictionaries?

1

4 Answers 4

1

Among the Python builtin types, it's true for dictionaries and sets. Lists and tuples preserve order. There is collections.OrderedDict for an ordered version of a dictionary. For other types (e.g., ones from libraries that aren't built into Python), you just have to read the documentation. There's no general rule for what a "data structure" does in Python. You have to look at the documentation of each type to understand what behavior it does or doesn't define.

Python does define the notion of "sequence", which is defined to have order (lists and tuples are sequences). A dictionary is a "mapping", which needn't have order. (See the Python glossary and the collections module for more info.)

As to why, it's just how dictionaries were implemented. Basically they can be faster if they don't have to keep track of order, and in many cases you don't care about the order, so they were implemented as unordered collections for efficiency.

Sign up to request clarification or add additional context in comments.

Comments

1

Both dicts and sets in python lose order. This is because they are implemented as hash tables and therefore more concerned with faster lookup times than with order preservation.

If you are looking for a data structure that's geared towards order-preservation, then you should look at lists. In your case, you could use a list of tuples as follows:

In [255]: L = []

In [256]: L.append((1,10))

In [257]: L.append((2,20))

In [258]: L.append((3,30))

In [259]: L
Out[259]: [(1, 10), (2, 20), (3, 30)]

If however, you want to preserve order and want faster lookup times than what list has to offer, then you're likely better off with an OrderedDict:

In [265]: d = collections.OrderedDict()

In [266]: d[1]=10

In [267]: d
Out[267]: OrderedDict([(1, 10)])

In [268]: d[2]=20

In [269]: d
Out[269]: OrderedDict([(1, 10), (2, 20)])

In [270]: d[3]=30

In [271]: d
Out[271]: OrderedDict([(1, 10), (2, 20), (3, 30)])

Hope this helps

Comments

0

Yes, it is only dictionaries. underneath, a dictionary does not in fact store the values as-is, but as a hash of the key paired with the value. This allows very fast lookups. Lists and tuples maintain order.

Comments

0

Dictionaries will order their entries to make searching for keys efficient. If you want to keep your keys in the same order they were added, try an OrderedDict.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.