Julia: Guarantee consistent order of pairs, keys and values in Dicts

Created on 19 Jul 2018  路  3Comments  路  Source: JuliaLang/julia

There are multiple ways to iterate a Dict. The functions keys(dict) and values(dict) are guaranteed to return items in a consistent order, as stated in the docs (https://docs.julialang.org/en/latest/base/collections/#Base.keys):

But keys(a) and values(a) both iterate a and return the elements in the same order.

This is of course assuming that the Dict has not been modified in between. However it is not asserted that for (k,v) in dict ... also iterates items in the same order as keys(dict) and values(dict). If this is guaranteed, it should be mentioned in the docs.

collections doc

Most helpful comment

The question is about whether iterating pairs, keys and values will be consistent given that the dict has not been modified in between. That is the case and I have a hard time imagining a way violations that would be beneficial, so I can鈥檛 see any harm in guaranteeing it. @jeffbezanson?

All 3 comments

I could do a PR myself. But first can someone confirm that this is guaranteed?
Also, in what section of the docs should this clarification go?

IIUC there is currently no order guarantee, hence why it is not documented. This PR has an OrderedDict that was considered for a little while to become the default #10116, but I believe the change was not made because we don't want to be limited by that choice in the future.

The question is about whether iterating pairs, keys and values will be consistent given that the dict has not been modified in between. That is the case and I have a hard time imagining a way violations that would be beneficial, so I can鈥檛 see any harm in guaranteeing it. @jeffbezanson?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Keno picture Keno  路  3Comments

felixrehren picture felixrehren  路  3Comments

wilburtownsend picture wilburtownsend  路  3Comments

arshpreetsingh picture arshpreetsingh  路  3Comments

dpsanders picture dpsanders  路  3Comments