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
dict.fromkeys insertion order #90066
Comments
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:
|
All the dict methods retain insertion order. There is nothing special about fromkeys(). |
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, |
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)). |
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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: