Skip to content

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

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

Closed
Yzi-Li opened this issue May 4, 2025 · 8 comments
Closed

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

Yzi-Li opened this issue May 4, 2025 · 8 comments
Labels
docs Documentation in the Doc dir

Comments

@Yzi-Li
Copy link
Contributor

Yzi-Li commented May 4, 2025

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

@Yzi-Li Yzi-Li added the docs Documentation in the Doc dir label May 4, 2025
@skirpichev
Copy link
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.

@skirpichev skirpichev added the pending The issue will be closed if no feedback is provided label May 4, 2025
@Yzi-Li
Copy link
Contributor Author

Yzi-Li commented May 4, 2025

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

@skirpichev
Copy link
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?

@Yzi-Li
Copy link
Contributor Author

Yzi-Li commented May 4, 2025

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.

@Yzi-Li
Copy link
Contributor Author

Yzi-Li commented May 4, 2025

Would it be better if we explain for it?

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

@skirpichev
Copy link
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.

@skirpichev skirpichev closed this as not planned Won't fix, can't repro, duplicate, stale May 4, 2025
@skirpichev skirpichev removed the pending The issue will be closed if no feedback is provided label May 4, 2025
@terryjreedy
Copy link
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.

@terryjreedy terryjreedy reopened this May 4, 2025
@Yzi-Li
Copy link
Contributor Author

Yzi-Li commented May 4, 2025

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

terryjreedy added a commit that referenced this issue May 7, 2025
Also move up the explanation of insertion order preservation.  Both paragraphs seemed out of place down where they were.
---------

Co-authored-by: Terry Jan Reedy <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 7, 2025
…ythonGH-133424)

Also move up the explanation of insertion order preservation.  Both paragraphs seemed out of place down where they were.
---------
(cherry picked from commit 61ac88c)

Co-authored-by: Yongzi Li <[email protected]>
Co-authored-by: Terry Jan Reedy <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 7, 2025
…ythonGH-133424)

Also move up the explanation of insertion order preservation.  Both paragraphs seemed out of place down where they were.
---------
(cherry picked from commit 61ac88c)

Co-authored-by: Yongzi Li <[email protected]>
Co-authored-by: Terry Jan Reedy <[email protected]>
terryjreedy added a commit that referenced this issue May 7, 2025
…H-133424) (#133620)

gh-133361: move the explanation of dict equal before its use (GH-133424)

Also move up the explanation of insertion order preservation.  Both paragraphs seemed out of place down where they were.
---------
(cherry picked from commit 61ac88c)

Co-authored-by: Yongzi Li <[email protected]>
Co-authored-by: Terry Jan Reedy <[email protected]>
terryjreedy added a commit that referenced this issue May 7, 2025
…H-133424) (#133621)

gh-133361: move the explanation of dict equal before its use (GH-133424)

Also move up the explanation of insertion order preservation.  Both paragraphs seemed out of place down where they were.
---------
(cherry picked from commit 61ac88c)

Co-authored-by: Yongzi Li <[email protected]>
Co-authored-by: Terry Jan Reedy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
Status: Todo
Development

No branches or pull requests

3 participants