thelist = ['a','b','c','d']
How I can to scramble them in Python?
thelist = ['a','b','c','d']
How I can to scramble them in Python?
>>> import random
>>> thelist = ['a', 'b', 'c', 'd']
>>> random.shuffle(thelist)
>>> thelist
['d', 'a', 'c', 'b']
Your result will (hopefully!) vary.
Use the shuffle
function from the random
module:
>>> from random import shuffle
>>> thelist = ['a','b','c','d']
>>> shuffle(thelist)
>>> thelist
['c', 'a', 'b', 'd']
in-place shuffle (modifies v, returns None)
random.shuffle(v)
not in-place shuffle (if you don't want to modify the original array, creates a shuffled copy)
v = random.sample(v, len(v))