Is there a canonical way to check whether an iterable is ordered?
rosuav at gmail.com
Fri Sep 19 14:06:47 CEST 2014
On Fri, Sep 19, 2014 at 9:58 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> All it says is that keys/values/items will correspond, not keys/keys, etc.
> Hmmm. On second thought, there is a problem with this:
> a = list(mydict)
> c = list(mydict.items()) # now the internal order is shuffled
> b = list(mydict.values())
> assert list(zip(a, b)) == c
> and the assertion fails.
> So I guess that rules out dict internal order changing during a single run
> of the interpreter, unless the dict is modified.
Precisely. If you iterate keys, keys, values with no mutations in
between, the first keys has to correspond to values, and the second
keys has to correspond to values, ergo they must correspond to each
> Anyway, that's just dicts. Custom iterables can change any time they like.
Oh, sure. That's a very specific promise of the dict, nothing else.
The set might well randomize, if it wished. But we still come back to
there being no reason for it to change.
More information about the Python-list