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

Chris Angelico rosuav at gmail.com
Fri Sep 19 04:45:11 CEST 2014


On Fri, Sep 19, 2014 at 9:52 AM, Roy Smith <roy at panix.com> wrote:
> In article <mailman.14103.1411047208.18130.python-list at python.org>,
>  Chris Angelico <rosuav at gmail.com> wrote:
>
>> The one thing you can rely on (and therefore must comply with, when
>> you design an iterable) is that iteration will hit every element
>> exactly once.
>
> Does it actually say that somewhere?  For example:
>
> for i in bag.pick_randomly_with_replacement(n=5):
>    print i
>
> shouldn't do that.

When you pick randomly from a population, you create a new population,
which may have duplicates compared to the original. (For efficiency's
sake it probably won't all actually exist immediately, but
conceptually it does exist.) That's what you're iterating over - not
the bag itself.

ChrisA



More information about the Python-list mailing list