Why does random.shuffle return None?

alvas picture alvas · Jul 15, 2013 · Viewed 70.5k times · Source

Why is random.shuffle returning None in Python?

>>> x = ['foo','bar','black','sheep']
>>> from random import shuffle
>>> print shuffle(x)
None

How do I get the shuffled value instead of None?

Answer

Martijn Pieters picture Martijn Pieters · Jul 15, 2013

random.shuffle() changes the x list in place.

Python API methods that alter a structure in-place generally return None, not the modified data structure.

If you wanted to create a new randomly-shuffled list based of an existing one, where the existing list is kept in order, you could use random.sample() with the full length of the input:

x = ['foo', 'bar', 'black', 'sheep']
random.sample(x, len(x))     

You could also use sorted() with random.random() for a sorting key:

shuffled = sorted(x, key=lambda k: random.random())

but this invokes sorting (an O(NlogN) operation), while sampling to the input length only takes O(N) operations (the same process as random.shuffle() is used, swapping out random values from a shrinking pool).

Demo:

>>> import random
>>> x = ['foo', 'bar', 'black', 'sheep']
>>> random.sample(x, len(x))
['bar', 'sheep', 'black', 'foo']
>>> sorted(x, key=lambda k: random.random())
['sheep', 'foo', 'black', 'bar']
>>> x
['foo', 'bar', 'black', 'sheep']