9
$\begingroup$

Mathematica 10 has introduced Associations, elsewhere known as hash tables or dictionaries. Is there Ordered equivalent like in Python, Java, .NET? The data structure should remember the order in which key-value is inserted.

https://docs.python.org/2/library/collections.html

http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html

https://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary(v=vs.110).aspx

$\endgroup$
4
  • 8
    $\begingroup$ Associations are ordered. $\endgroup$ Commented Mar 27, 2015 at 2:26
  • 1
    $\begingroup$ @Leonid Shifrin, any reference? I could not find. $\endgroup$
    – den.run.ai
    Commented Mar 27, 2015 at 2:28
  • 3
    $\begingroup$ Apparently, this seems to not be mentioned in the docs explicitly, at least not on the main Association doc page. One can sort of figure this out by the fact that one can use Part with numerical indices on associations, but such fundamental property should be mentioned explicitly. I will file a suggestion report. $\endgroup$ Commented Mar 27, 2015 at 11:15
  • 2
    $\begingroup$ Not exactly a reference, but Sort and KeySort return sorted associations (and that's documented behavior). That wouldn't make any sense if associations were unordered. $\endgroup$ Commented Apr 5, 2015 at 11:25

2 Answers 2

11
$\begingroup$

That associations are ordered can be easily demonstrated.

assoc = <|a -> x, b -> y, c -> z|>;

assoc[d] = 42; assoc
<|a -> x, b -> y, c -> z, d -> 42|>
assoc[a] = w; assoc
<|a -> w, b -> y, c -> z, d -> 42|>

Since the key d did not exist, the key, value pair d -> 42 was added to the end of the association. On the hand since the key a did exist, it's position was preserved.

$\endgroup$
3
  • 2
    $\begingroup$ assoc = <|b -> x, c -> y, d -> z|>; assoc[a] = 42; assoc (* <|b -> x, c -> y, d -> z, a -> 42|> *), so order in Association means insertion order, as opposed to the infinite universe of orders (reflexive, antisymmetric relation) that one might imagine, eg lex order. $\endgroup$ Commented Mar 27, 2015 at 3:49
  • 1
    $\begingroup$ @m_goldberg, is this guaranteed behavior? You did not show operations such as deletion of key-value pairs $\endgroup$
    – den.run.ai
    Commented Mar 27, 2015 at 20:31
  • $\begingroup$ @denfromufa. I can't guarantee any behavior, but all the ways I know of that can delete a pair preserve the order. Obviously some operations, such as Sort, KeySort, etc., don't preserve order because their purpose is to change it. I would recommend that you just try any operation you are suspicious of and see what happens. Mathematica is so interactive that is trivial to make such experiments. $\endgroup$
    – m_goldberg
    Commented Mar 27, 2015 at 21:43
1
$\begingroup$

The fast introduction for programmers on associations with additional notes for Python:

NOTES FOR PYTHON PROGRAMMERS:

Associations in the Wolfram Language work similarly to Python dictionaries. Using an Association, programmers can associate keys to values with highly efficient lookup and updating, even with millions of elements, and they also preserve the order of element insertion without needing specialized data structures like Python's OrderedDict. When coding in Python with the Wolfram Client Library for Python, classes like dict, OrderedDict and Series (from the Pandas library) serialize to Association.

To be precise Python 3.7 (2018) maintains insertion order for dicts as well: https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.