Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Does oc.dict.values preserve order? #982

Closed
will-gp opened this issue Jul 21, 2022 · 2 comments
Closed

Does oc.dict.values preserve order? #982

will-gp opened this issue Jul 21, 2022 · 2 comments
Labels
question Request for help, not a bug or feature request

Comments

@will-gp
Copy link

will-gp commented Jul 21, 2022

I am wondering if oc.dict.values preserves order.

Given the following example:

pets.yaml

zebra:
    - name: Bob
    - age: 24
cat:
    - name: Louis
    - age: 5

and another yaml file which uses pets.yaml:

defaults:
  - pets

profile:
   - ${oc.dict.values:pets}

will oc.dict.values preserve the order of the objects in pets.yaml?
ie. the order will always be the values of [zebra,cat] and never [cat, zebra]

@Jasha10
Copy link
Collaborator

Jasha10 commented Jul 21, 2022

TLDR: Yes, oc.dict.{keys,values} does iterate over keys/values in order (for python version >= 3.7)


oc.dict.keys and oc.dict.values iterate over the keys/values in the same order that they appear in the DictConfig object. (You can see the implementation of oc.dict.keys and oc.dict.values in this file) Under the hood, DictConfig uses a python dictionary to store its contents (i.e. its keys and values). For Python version >=3.7, python dictionaries are guaranteed to preserve order of keys/values. This means that, for python >= 3.7, DictConfig itself is stable in terms of the order of its keys, and oc.dict.{keys,values} will preserve that order.

I noticed that your example above uses yaml files and a hydra-like defaults list. Note that according to the yaml spec, yaml mappings (i.e. "yaml dicts") are technically unordered. OmegaConf and Hydra use the pyyaml library to handle yaml parsing, and I'm not sure if pyyaml guarantees that the keys/values from yaml files will be parsed in order. In any case, looking at your Hydra example above, the order of cfg.pets.values() will be the same as the order of cfg.profile[0], even if that order is backwards when compared with the order of keys in the source yaml file pets.yaml.

@will-gp
Copy link
Author

will-gp commented Jul 22, 2022

Thanks @Jasha10. It does seem, based on a number of discussions, that pyyaml preserves insertion order in python >= 3.7. In this case, even though the yaml mappings would be ordered, this would still represent a valid yaml.

I've included the link(s) below for reference:
yaml/pyyaml#110
yaml/pyyaml#523

I would note that although loading the yaml preserves order, yaml.dump() does not, which is a source of confusion for many.

@Jasha10 Jasha10 added the question Request for help, not a bug or feature request label Jul 29, 2022
Repository owner locked and limited conversation to collaborators Jul 29, 2022
@Jasha10 Jasha10 converted this issue into discussion #985 Jul 29, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Request for help, not a bug or feature request
Projects
None yet
Development

No branches or pull requests

2 participants