tags:

views:

268

answers:

4
thelist = ['a','b','c','d']

What's the best way to scramble them in Python?

+3  A: 

Use the random.shuffle() function:

random.shuffle(thelist)
Greg Hewgill
+2  A: 

Use the shuffle function from the random module:

>>> from random import shuffle
>>> thelist = ['a','b','c','d']
>>> shuffle(thelist)
>>> thelist
['c', 'a', 'b', 'd']
RichieHindle
Hey, how come you got different output to Peter? ;)
Dominic Rodger
Who else went straight to the 'Add Comment' button to lambast, before seeing the wink? :P
Dominic Bou-Samra
+4  A: 
import random
random.shuffle(thelist)

Note, this shuffles the list in-place.

Phil
+1 for "in place" .
Aaron Digulla
+8  A: 
>>> import random
>>> thelist = ['a', 'b', 'c', 'd']
>>> random.shuffle(thelist)
>>> thelist
['d', 'a', 'c', 'b']

Your result will (hopefully!) vary.

Peter