152

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?

alvas
  • 115,346
  • 109
  • 446
  • 738
  • 5
    Related: [sort() and reverse() functions do not work](http://stackoverflow.com/q/16460616) – Martijn Pieters Jul 15 '13 at 08:57
  • 2
    Also related: [Why doesn't calling a Python string method do anything unless you assign its output?](https://stackoverflow.com/questions/9189172/why-doesnt-calling-a-python-string-method-do-anything-unless-you-assign-its-out) – smci May 01 '18 at 05:40
  • 1
    Related: [Why do these list methods (append, sort, extend, remove, clear, reverse) return None rather than the resulting list?](/q/11205254/4518341) – wjandrea Mar 06 '23 at 18:22

5 Answers5

251

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.

>>> x = ['foo', 'bar', 'black', 'sheep']
>>> random.shuffle(x)
>>> x
['black', 'bar', 'sheep', 'foo']

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

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(N log N) 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']
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    Is using a random-valued `key` function truly guaranteed? Some fast sorting algorithms fall over if comparisons are not self-consistent. I can see this working either way, depending on implementation (decorate-sort-undecorate will only need to apply `key` once on each element so will be well-defined). – torek Jul 15 '13 at 09:52
  • 2
    @torek: Python uses decorate-sort-undecorate when sorting with a `key` callable. So yes, it’s guaranteed as each value is given their random key exactly once. – Martijn Pieters Jul 15 '13 at 16:23
51

This method works too.

import random
shuffled = random.sample(original, len(original))
Asclepius
  • 57,944
  • 17
  • 167
  • 143
Acemad
  • 3,241
  • 3
  • 23
  • 29
13

Why, really?

1. Efficiency

shuffle modifies the list in place. This is nice, because copying a large list would be pure overhead if you do not need the original list anymore.

2. Pythonic style

According to the "explicit is better than implicit" principle of pythonic style, returning the list would be a bad idea, because then one might think it is a new one although in fact it is not.

But I don't like it like this!

If you do need a fresh list, you will have to write something like

new_x = list(x)  # make a copy
random.shuffle(new_x)

which is nicely explicit. If you need this idiom frequently, wrap it in a function shuffled (see sorted) that returns new_x.

Lutz Prechelt
  • 36,608
  • 11
  • 63
  • 88
8

According to docs:

Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random().

>>> x = ['foo','bar','black','sheep']
>>> from random import shuffle
>>> shuffle(x)
>>> x
['bar', 'black', 'sheep', 'foo']
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
2

I had my aha moment with this concept like this:

from random import shuffle
x = ['foo','black','sheep'] #original list
y = list(x) # an independent copy of the original
for i in range(5):
    print shuffle(y) # shuffles the original "in place" prints "None" return
    print x,y #prints original, and shuffled independent copy

>>>
None
['foo', 'black', 'sheep'] ['foo', 'black', 'sheep']
None
['foo', 'black', 'sheep'] ['black', 'foo', 'sheep']
None
['foo', 'black', 'sheep'] ['sheep', 'black', 'foo']
None
['foo', 'black', 'sheep'] ['black', 'foo', 'sheep']
None
['foo', 'black', 'sheep'] ['sheep', 'black', 'foo']
litepresence
  • 3,109
  • 1
  • 27
  • 35
  • Because python does "copy-by-values" by default instead of pass-by-reference =) http://stackoverflow.com/a/986495/610569 – alvas Mar 20 '17 at 00:54