Sorted list - how to change it

Wolfram Kraus kraus at hagen-partner.de
Thu Nov 9 04:55:45 EST 2006


On 09.11.2006 10:52, Lad wrote:
> I have a sorted list for example [1,2,3,4,5] and I would like to change
> it in a random way
> e.g [2,5,3,1,4] or [3,4,1,5,2]  or in any other way except being
> ordered.
> What is the best/easiest
>  way how to do it?
> 
> Thank you for help
> L.
> 
use random.shuffel:

>>> import random
>>> x = [1,2,3,4,5]
>>> random.shuffle(x)
>>> x
[1, 4, 2, 3, 5]
>>> random.shuffle(x)
>>> x
[4, 2, 1, 3, 5]

see: http://docs.python.org/lib/module-random.html

HTH,
Wolfram



More information about the Python-list mailing list