Mailing List Archive

[issue45908] dict.fromkeys insertion order
New submission from Vedran ?a?i? <vedgar@gmail.com>:

I'm sure this is exactly how it should work, I just want to know if you think it is documented properly, so I can rely on it. In my opinion the docs should be more precise.

>>> ''.join(dict.fromkeys('axbxc'))
'axbc'

Is this guaranteed by the documentation? Of course, dict iteration order is now guaranteed to be insertion order, but still, nowhere do the docs say that fromkeys inserts the keys into new dictionary in order in which they appear in its argument.

(Probably the reason for this is that dict iteration order was fixed in 3.7, yet fromkeys existed a long time before that.)

I propose an addition to the documentation:

> Create a new dictionary with keys from iterable (in order) and values set to value.

https://docs.python.org/3/library/stdtypes.html

----------
assignee: docs@python
components: Documentation
messages: 407136
nosy: docs@python, veky
priority: normal
severity: normal
status: open
title: dict.fromkeys insertion order
versions: Python 3.10, Python 3.11

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue45908>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue45908] dict.fromkeys insertion order [ In reply to ]
Change by Dong-hee Na <donghee.na@python.org>:


----------
nosy: +methane, rhettinger

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue45908>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue45908] dict.fromkeys insertion order [ In reply to ]
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:

All the dict methods retain insertion order. There is nothing special about fromkeys().

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue45908>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue45908] dict.fromkeys insertion order [ In reply to ]
Vedran ?a?i? <vedgar@gmail.com> added the comment:

Absolutely, but that's not my problem. I take your sentence to mean that when I do something with a _dict_ argument, it should try to preserve its insertion order as much as possible (given the semantics of the concrete method in question). I agree.

But my question is about constructing a dict from something other than a dict (here, a str, simply because it's easiest to visualize). I'm sure you don't mean to say dict.fromkeys retains the insertion order of its argument always, since it's obviously false if you give it a set.

What I'd like to be specified here (or elsewhere, but here I think it's useful) is that _iteration order_ of the argument to dict.fromkeys is preserved as _insertion order_ (and therefore iteration order) of the resulting dict. Besides, I don't see any other point where it should be specified... the only other constructor, `dict` itself, gives a very precise description (https://docs.python.org/3/library/stdtypes.html#dict) of how it creates a dict from its argument(s). Of course, there it mattered even before Py3.7, since values were important. In dict.fromkeys values are all the same, but order still matters and should (IMO) be specified.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue45908>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue45908] dict.fromkeys insertion order [ In reply to ]
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:

Thanks for the suggestion but I’m going to decline. We has many APIS that consume an iterable and all of them do so In iteration order. Even the regular dict() constructor takes an iterable of tuples and adds them in iteration order. Also, I’m not concerned because of our experience with OrderedDict() which for a decade had a fromkeys() method and there has never been a question about it. There was even an idiom for deducing a list while maintaining order: list(OrderedDict.fromkeys(seq)).

----------
resolution: -> not a bug
stage: -> resolved
status: open -> closed

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue45908>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue45908] dict.fromkeys insertion order [ In reply to ]
Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:

What else can it be? dict.fromkeys() adds keys in the order of obtaining them, and it obtains them by iterating its argument. If we need a special note here, we need a special note for list(), tuple(), filter(), enumerate() and all other functions which consume an iterable.

----------
nosy: +serhiy.storchaka

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue45908>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue45908] dict.fromkeys insertion order [ In reply to ]
Change by Raymond Hettinger <raymond.hettinger@gmail.com>:


----------
assignee: docs@python -> rhettinger

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue45908>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com