Skip to content

the order of dict isn't documented in an example #133361

Closed

Description

@Yzi-Li

Documentation

I am reading the documentation of dictionary.
There is an example

>>> a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
>>> f = dict({'one': 1, 'three': 3}, two=2)
>> a == b == c == d == e == f
True

For new users, they might not know that the dictionary is unorderd in 3.7+ (not everyone reads data model before reading this). So they might feel confused and think {one: 1, two: 2} shoudn't be equal to {two:2, one:1}.

Maybe we could link date model for explaning.

Linked PRs

Activity

  1. added
    docs
    on May 4, 2025
  2. skirpichev commented on May 4, 2025

    @skirpichev
    Member

    So they might feel confused and think {one: 1, two: 2} shoudn't be equal to {two:2, one:1}.

    Why? They could read the rest of dict docs on same page:

    Dictionaries compare equal if and only if they have the same (key, value) pairs (regardless of ordering). Order comparisons (‘<’, ‘<=’, ‘>=’, ‘>’) raise TypeError.

  3. added
    pending
    on May 4, 2025
  4. Yzi-Li commented on May 4, 2025

    @Yzi-Li
    ContributorAuthor

    Oh, it mentions in values() and I just realized this. Would it be better if we explain for it?

  5. skirpichev commented on May 4, 2025

    @skirpichev
    Member

    Oh, it mentions in values() and I just realized this.

    It's just after description of dict's methods.

    Would it be better if we explain for it?

    Explain what? What's unclear in the above sentence?

  6. Yzi-Li commented on May 4, 2025

    @Yzi-Li
    ContributorAuthor

    It's just after description of dict's methods.

    Yes, you're right. I didn't notice the indent.

    Explain what? What's unclear in the above sentence?

    I'm not sure if it is right. Some important things like the order of dict should say first. It shouldn't be after its methods. This might be more friendly for new users.

  7. Yzi-Li commented on May 4, 2025

    @Yzi-Li
    ContributorAuthor

    Would it be better if we explain for it?

    "it" refers to the example instead of the above sentence.

  8. skirpichev commented on May 4, 2025

    @skirpichev
    Member

    "it" refers to the example instead of the above sentence.

    Ok. Examples here are to illustrate dict() constructor. And there are several cases, which differ by ordering of keys. I doubt that adding something does make sense here.

  9. removed
    pending
    on May 4, 2025
  10. terryjreedy commented on May 4, 2025

    @terryjreedy
    Member

    Dicts became ordered (by insertion), not unordered, in 3.6/7. And it is documented: Dictionaries preserve insertion order. Note that updating a key does not affect the order. Keys added after deletion are inserted at the end." This is followed by examples and version note. Nothing more is needed.

    I think the following explains equality quite sufficiently, but should be moved before the example.

    Dictionaries compare equal if and only if they have the same (key, value) pairs (regardless of ordering). Order comparisons (‘<’, ‘<=’, ‘>=’, ‘>’) raise TypeError.

    I am reopening to move the explanation of == before its use.

  11. Yzi-Li commented on May 4, 2025

    @Yzi-Li
    ContributorAuthor

    Do you need me to create a PR to move this explanation?

  12. added a commit that references this issue on May 7, 2025
  13. added 2 commits that reference this issue on May 7, 2025
  14. added 2 commits that reference this issue on May 7, 2025
  15. added a commit that references this issue on Jul 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions