thelist = ['a','b','c','d']
What's the best way to scramble them in Python?
thelist = ['a','b','c','d']
What's the best way to scramble them in Python?
Use the shuffle
function from the random
module:
>>> from random import shuffle
>>> thelist = ['a','b','c','d']
>>> shuffle(thelist)
>>> thelist
['c', 'a', 'b', 'd']
import random
random.shuffle(thelist)
Note, this shuffles the list in-place.
>>> import random
>>> thelist = ['a', 'b', 'c', 'd']
>>> random.shuffle(thelist)
>>> thelist
['d', 'a', 'c', 'b']
Your result will (hopefully!) vary.