This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dict.fromkeys insertion order
Type: Stage: resolved
Components: Documentation Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: docs@python, methane, rhettinger, serhiy.storchaka, veky
Priority: normal Keywords:

Created on 2021-11-27 08:02 by veky, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg407136 - (view) Author: Vedran Čačić (veky) * Date: 2021-11-27 08:02
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
msg407146 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-11-27 14:40
All the dict methods retain insertion order. There is nothing special about fromkeys().
msg407153 - (view) Author: Vedran Čačić (veky) * Date: 2021-11-27 16:25
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.
msg407166 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-11-27 20:39
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)).
msg407167 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-11-27 20:41
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.
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 90066
2021-11-27 20:42:15rhettingersetassignee: docs@python -> rhettinger
2021-11-27 20:41:02serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg407167
2021-11-27 20:39:10rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg407166

stage: resolved
2021-11-27 16:25:15vekysetmessages: + msg407153
2021-11-27 14:40:53rhettingersetmessages: + msg407146
2021-11-27 08:29:14corona10setnosy: + rhettinger, methane
2021-11-27 08:02:49vekycreate