[Python-ideas] About adding a new iterator methodcalled "shuffled"

Steven D'Aprano steve at pearwood.info
Tue Mar 24 22:40:11 CET 2009


On Wed, 25 Mar 2009 07:20:00 am Steven D'Aprano wrote:
> On Wed, 25 Mar 2009 03:03:31 am Raymond Hettinger wrote:
> > > On Tue, Mar 24, 2009, Roy Hyunjin Han wrote:
> > >> I know that Python has iterator methods called "sorted" and
> > >> "reversed" and these are handy shortcuts.
> > >>
> > >> Why not add a new iterator method called "shuffled"?
> >
> > You can already write:
> >
> >        sorted(s, key=lambda x: random())
> >
> > But nobody does that.  So you have a good
> > indication that the proposed method isn't needed.
>
> That's nice -- not as readable as random.shuffle(s) but still nice.
> And fast too: on my PC, it is about twice as fast as random.shuffle()
> for "reasonable" sized lists (tested up to one million items).

Ah crap. Ignore the above: I made an embarrassing error in my test 
(neglected to actually call random inside the lambda) and so my timings 
were completely wrong. The current random.shuffle() is marginally 
faster even for small lists (500 items) so I withdraw my suggestion 
that it be replaced.



-- 
Steven D'Aprano



More information about the Python-ideas mailing list