import random
a = [1,2,3,4,5,6,7,8,9]
# random.shuffle(a) # this is fine
a = random.shuffle(a) # a is now None
I made a typo in my code but I'd like to know in this case why a turns into None. What's going on under the hood?
import random
a = [1,2,3,4,5,6,7,8,9]
# random.shuffle(a) # this is fine
a = random.shuffle(a) # a is now None
I made a typo in my code but I'd like to know in this case why a turns into None. What's going on under the hood?
random.shuffle
works in-place, it does not return anything (And hence by default it returns None
, since all Python function calls should return some value) , hence when you do - a = random.shuffle(a)
, a
becomes None. Try -
random.shuffle(a)
From documentation of random.shuffle
-
random.shuffle(x[, random])
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().
(Emphasis mine)