On Sat, Aug 1, 2020 at 6:07 AM Ricky Teachey email@example.com wrote:
On Fri, Jul 31, 2020 at 3:52 PM Marco Sulla Marco.Sulla.Python@gmail.com wrote:
Is it not more simple to add some sequence methods to the dict views (if there's a real need)? If you do tuple(dict.keys()), you get the sequence of keys in the same insertion order of the dict. It seems to me that the duck already exists and quacks.
The problem with that is these view objects are in fact NOT ducks (sequences). They are gooses (sets). Gooses don't quack. They honk.
They are guaranteed to iterate in the same order as the dict, though, unless I'm mistaken. There's been a weaker guarantee for a long time (that iteration order for keys/values/items is consistent and will not change without a dict mutation), and I can't find anywhere that it's now guaranteed to be the same as dict order, but I would be very surprised if list(d) was different from list(d.keys()). So, constructing a tuple or list from the keys or items WILL give you a sequence.
Whether that's good enough is another question. For instance, it requires taking a full copy of the dict, even if all you want is a single element from it.