Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78010 > unrolled thread

Is there a canonical way to check whether an iterable is ordered?

Started bycool-RR <ram.rachum@gmail.com>
First post2014-09-18 04:55 -0700
Last post2014-09-19 15:04 +1000
Articles 2 on this page of 22 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Is there a canonical way to check whether an iterable is ordered? cool-RR <ram.rachum@gmail.com> - 2014-09-18 04:55 -0700
    Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-18 22:10 +1000
      Re: Is there a canonical way to check whether an iterable is ordered? Roy Smith <roy@panix.com> - 2014-09-18 08:58 -0400
        Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-18 23:33 +1000
          Re: Is there a canonical way to check whether an iterable is ordered? Roy Smith <roy@panix.com> - 2014-09-18 19:52 -0400
            Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 12:45 +1000
            Re: Is there a canonical way to check whether an iterable is ordered? Terry Reedy <tjreedy@udel.edu> - 2014-09-19 18:02 -0400
            Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-20 15:01 +1000
        Re: Is there a canonical way to check whether an iterable is ordered? Terry Reedy <tjreedy@udel.edu> - 2014-09-18 09:46 -0400
        Re: Is there a canonical way to check whether an iterable is ordered? Tim Chase <python.list@tim.thechases.com> - 2014-09-18 09:32 -0500
        Re: Is there a canonical way to check whether an iterable is ordered? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-19 15:15 +1000
          Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 15:40 +1000
            Re: Is there a canonical way to check whether an iterable is ordered? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-19 20:59 +1000
              Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 21:19 +1000
                Re: Is there a canonical way to check whether an iterable is ordered? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-19 21:58 +1000
                  Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 22:06 +1000
              Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 21:25 +1000
                Re: Is there a canonical way to check whether an iterable is ordered? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-19 21:46 +1000
                  Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 21:56 +1000
                    Re: Is there a canonical way to check whether an iterable is ordered? alister <alister.nospam.ware@ntlworld.com> - 2014-09-19 12:26 +0000
                      Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 22:36 +1000
    Re: Is there a canonical way to check whether an iterable is ordered? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-19 15:04 +1000

Page 2 of 2 — ← Prev page 1 [2]


#78077

FromChris Angelico <rosuav@gmail.com>
Date2014-09-19 22:36 +1000
Message-ID<mailman.14145.1411130223.18130.python-list@python.org>
In reply to#78076
On Fri, Sep 19, 2014 at 10:26 PM, alister
<alister.nospam.ware@ntlworld.com> wrote:
> As far as I understand it the order of keys in a dict is not guaranteed
> iterating over the same dict twice (without changes) does not have to
> return the keys in the same order.

The exact guarantee is that you can iterate over keys() followed by
values() and they will correspond. This implies (in the strict logical
sense of the word "implies", as well as the common sense of the same
word) that iterating multiple times over keys() will correspond, which
is the point I drew above.

ChrisA

[toc] | [prev] | [next] | [standalone]


#78057

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-09-19 15:04 +1000
Message-ID<541bb974$0$6599$c3e8da3$5496439d@news.astraweb.com>
In reply to#78010
cool-RR 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?

See the collections.abc module:

https://docs.python.org/3/library/collections.abc.html

I think what you want is:

import collections.abc
isinstance(it, collections.abc.Sequence)

Prior to 3.3, you would use:

# Untested.
import collections
isinstance(it, collections.Sequence)



-- 
Steven

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web