Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'modified': 0.07; 'prevents': 0.09; 'subject:iterable': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '-tkc': 0.16; 'collections': 0.16; 'first:': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterable': 0.16; 'iterable:': 0.16; 'iterating': 0.16; 'iterators,': 0.16; 'received:174.136': 0.16; 'roy': 0.16; 'sorting': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.18; 'meant': 0.20; 'cc:addr:python.org': 0.22; 'library,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'least': 0.26; 'header:In- Reply-To:1': 0.27; '[1]': 0.29; 'have,': 0.30; 'said,': 0.30; "i'm": 0.30; 'assert': 0.31; 'container': 0.31; 'sets.': 0.31; 'url:python': 0.33; "i'd": 0.34; 'test': 0.35; 'but': 0.35; 'i.e.': 0.36; 'ordered': 0.36; 'set.': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'similar': 0.36; 'url:org': 0.36; 'positive': 0.37; 'list': 0.37; 'list.': 0.37; 'received:10': 0.37; 'being': 0.38; 'generic': 0.38; 'url:library': 0.38; 'whatever': 0.38; 'track': 0.38; 'called': 0.40; 'tell': 0.60; 'skip:* 10': 0.61; "you're": 0.61; 'become': 0.64; 'talking': 0.65; 'between': 0.67; 'smith': 0.68; 'subject:there': 0.68; 'guaranteed': 0.75; '"not': 0.84; 'speedy': 0.84; 'subject:check': 0.84; 'url:items': 0.84; '"how': 0.91 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1411050851377:4181715324 X-MC-Ingress-Time: 1411050851377 Date: Thu, 18 Sep 2014 09:32:15 -0500 From: Tim Chase To: Roy Smith Subject: Re: Is there a canonical way to check whether an iterable is ordered? In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AuthUser: tim@thechases.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411059441 news.xs4all.nl 2914 [2001:888:2000:d::a6]:39756 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78027 On 2014-09-18 08:58, Roy Smith wrote: > 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. > > 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, For stdlib *collections*, yes, but if you're just talking generic iterators, then it can become exhausted in the first: with open('example.txt') as f: list1 = [item for item in f] list2 = [item for item in f] assert list1 == list2, "Not equal" The OP would have to track the meta-information regarding whether the iterable was sorted. At least for dicts, order is guaranteed by the specs as long as the container isn't modified between iterations[1], but I don't see any similar claim for sets. You can always test the thing: def foo(iterable): if isinstance(iterable, (set, frozenset)): iterable = sorted(iterable) for thing in iterable: do_stuff(thing) but nothing prevents that from being called with an unsorted list. That said, sorting in the stdlib is pretty speedy on pre-sorted lists, so I'd just start by sorting whatever it is that you have, unless you're positive it's already sorted. -tkc [1] https://docs.python.org/2/library/stdtypes.html#dict.items