Is there a canonical way to check whether an iterable is ordered?
Roy Smith
roy at panix.com
Thu Sep 18 08:58:47 EDT 2014
In article <mailman.14101.1411042251.18130.python-list at python.org>,
Chris Angelico <rosuav at gmail.com> wrote:
> On Thu, Sep 18, 2014 at 9:55 PM, cool-RR <ram.rachum at gmail.com> wrote:
> > My function gets an iterable of an unknown type. I want to check whether
> > it's ordered. I could check whether it's a `set` or `frozenset`, which
> > would cover many cases, but I wonder if I can do better. Is there a nicer
> > way to check whether an iterable is ordered or not?
> >
>
> An iterable is always ordered. You call next() and you get the next
> value.
I suspect what he meant was "How can I tell if I'm iterating over an
ordered collection?", i.e. iterating over a list vs. iterating over a
set.
Is there anything which requires an iterator to be deterministic? For
example, let's say I have an iterable, i, and I do:
list1 = [item for item in i]
list2 = [item for item in i]
am I guaranteed that list1 == list2? It will be for all the collections
I can think of in the standard library, but if I wrote my own class with
an __iter__() which yielded the items in a non-deterministic order,
would I be violating something other than the principle of least
astonishment?
More information about the Python-list
mailing list