Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guarantee consistent order of pairs, keys and values in Dicts #28196

Open
cossio opened this issue Jul 19, 2018 · 3 comments
Open

Guarantee consistent order of pairs, keys and values in Dicts #28196

cossio opened this issue Jul 19, 2018 · 3 comments
Labels
domain:collections Data structures holding multiple items, e.g. sets domain:docs This change adds or pertains to documentation

Comments

@cossio
Copy link
Contributor

cossio commented Jul 19, 2018

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.

@cossio cossio changed the title Iteration of dicts Order of iteration of Dicts Jul 19, 2018
@JeffBezanson JeffBezanson added the domain:docs This change adds or pertains to documentation label Jul 19, 2018
@ararslan ararslan added the domain:collections Data structures holding multiple items, e.g. sets label Jul 20, 2018
@cossio
Copy link
Contributor Author

cossio commented Jul 21, 2018

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?

@jebej
Copy link
Contributor

jebej commented Jul 21, 2018

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.

@StefanKarpinski
Copy link
Sponsor Member

StefanKarpinski commented Jul 21, 2018

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’t see any harm in guaranteeing it. @JeffBezanson?

@cossio cossio changed the title Order of iteration of Dicts Guarantee order of iteration of Dicts Jul 21, 2018
@cossio cossio changed the title Guarantee order of iteration of Dicts Guarantee consistent order of pairs, keys and values in Dicts Jul 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:collections Data structures holding multiple items, e.g. sets domain:docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

5 participants