Does anyone know a way to cross items in a list?

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

How can I scramble them in Python?

+5
source share
4 answers
>>> import random
>>> thelist = ['a', 'b', 'c', 'd']
>>> random.shuffle(thelist)
>>> thelist
['d', 'a', 'c', 'b']

Your result will (hopefully!) Change.

+13
source
import random
random.shuffle(thelist)

Note that this shuffles the list in place.

+12
source

Use function random.shuffle():

random.shuffle(thelist)
+6
source

Use the shufflefunction from the module random:

>>> from random import shuffle
>>> thelist = ['a','b','c','d']
>>> shuffle(thelist)
>>> thelist
['c', 'a', 'b', 'd']
+5
source

All Articles