Is there a canonical way to check whether an iterable is ordered?
rosuav at gmail.com
Fri Sep 19 13:19:53 CEST 2014
On Fri, Sep 19, 2014 at 8:59 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
>> """If keys, values and items views are iterated over with no
>> intervening modifications to the dictionary, the order of items will
>> directly correspond."""
>> So if iterating over d.keys() and then d.values() with no mutations is
>> guaranteed to give the same order, then so is iterating over d.keys(),
>> then d.keys(), then d.values(),
> Not so! So long as the iteration of values() matched the *second* iteration
> of keys(), it would be allowed. There's nothing which says that the first
> iteration of keys() has to match the second.
I disagree. Between the first iteration of keys() and the iteration of
values(), there were no modifications to the dictionary - another
iteration over keys() isn't a modification - ergo the guarantee ought
to hold. If you replace that inner iteration with the opening of a
file, or the division of a float by a Decimal, or the definition of a
new class, I'm sure everyone would agree that the guarantee still
holds, because they don't change the dict; so why should iterating
over keys() be any different?
More information about the Python-list