Trying to use sets for random selection, but the pop() method returns items in order

Carl Banks pavlovevidence at gmail.com
Wed Jul 1 21:46:10 EDT 2009


On Jul 1, 6:37 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Carl Banks <pavlovevide... at gmail.com> writes:
> > If you're iterating through the whole list and don't need to preserve
> > the original order (as was the case here) random.shuffle() is better.
>
> 1. Random.sample avoids iterating through the whole list when it can.
>
> 2. Say you want to choose 10 random numbers between 1 and 1000000.
>
>    random.sample(xrange(1000000), 10)
>
> works nicely.  Doing the same with shuffle is painful.

random.shuffle() is still better when you're iterating through the
whole list as the OP was doing.

Carl Banks



More information about the Python-list mailing list