Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a Dictionary "not ordered"?

I have read this in answer to many questions on here. But what exactly does it mean?

var test = new Dictionary<int, string>(); test.Add(0, "zero"); test.Add(1, "one"); test.Add(2, "two"); test.Add(3, "three");  Assert(test.ElementAt(2).Value == "two"); 

The above code seems to work as expected. So in what manner is a dictionary considered unordered? Under what circumstances could the above code fail?

like image 705
fearofawhackplanet Avatar asked Jun 17 '11 10:06

fearofawhackplanet


People also ask

Can a dictionary be ordered?

It is not possible to sort a dictionary, only to get a representation of a dictionary that is sorted. Dictionaries are inherently orderless, but other types, such as lists and tuples, are not. So you need an ordered data type to represent sorted values, which will be a list—probably a list of tuples.

Is dictionary an ordered sequence?

Dictionaries are insertion ordered as of Python 3.6. It is described as a CPython implementation detail rather than a language feature.

Is dictionary is ordered or unordered?

A dictionary is a collection which is ordered*, changeable and do not allow duplicates. As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

Does order matter in a dictionary?

Specifically, when you compare ordered dictionaries, the order of items matters. That's not the case with regular dictionaries.


2 Answers

Well, for one thing it's not clear whether you expect this to be insertion-order or key-order. For example, what would you expect the result to be if you wrote:

var test = new Dictionary<int, string>(); test.Add(3, "three"); test.Add(2, "two"); test.Add(1, "one"); test.Add(0, "zero");  Console.WriteLine(test.ElementAt(0).Value); 

Would you expect "three" or "zero"?

As it happens, I think the current implementation preserves insertion ordering so long as you never delete anything - but you must not rely on this. It's an implementation detail, and that could change in the future.

Deletions also affect this. For example, what would you expect the result of this program to be?

using System; using System.Collections.Generic;  class Test {      static void Main()      {         var test = new Dictionary<int, string>();         test.Add(3, "three");         test.Add(2, "two");         test.Add(1, "one");         test.Add(0, "zero");          test.Remove(2);         test.Add(5, "five");          foreach (var pair in test)         {             Console.WriteLine(pair.Key);         }     }      } 

It's actually (on my box) 3, 5, 1, 0. The new entry for 5 has used the vacated entry previously used by 2. That's not going to be guaranteed either though.

Rehashing (when the dictionary's underlying storage needs to be expanded) could affect things... all kinds of things do.

Just don't treat it as an ordered collection. It's not designed for that. Even if it happens to work now, you're relying on undocumented behaviour which goes against the purpose of the class.

like image 145
Jon Skeet Avatar answered Sep 28 '22 05:09

Jon Skeet


A Dictionary<TKey, TValue> represents a Hash Table and in a hashtable there is no notion of order.

The documentation explains it pretty well:

For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair structure representing a value and its key. The order in which the items are returned is undefined.

like image 44
Darin Dimitrov Avatar answered Sep 28 '22 03:09

Darin Dimitrov