0

so I am working on a py package and I have a prob.

when I run the following code, it prints none. pls help.

def shuff(list_):
    variable = random.shuffle(list_)
    return variable

lis = ["asad", "abaxj", "hccj", "xjx", "jcjz", "cbjbz"]
var = shuff(lis)
print(var)

any help is appreciated :)

1 Answers1

3

From the Python documentation on random.shuffle():

Shuffle the sequence x in place.

Therefore, the function does not return anything. Instead, the list you provide as argument is changed (it is shuffled). See for example the following code:

import random

x = [0, 1, 2, 3, 4]
print(x)  # prints '[0, 1, 2, 3, 4]'
random.shuffle(x)
print(x)  # prints '[4, 1, 3, 0, 2]'
luuk
  • 1,630
  • 4
  • 12
  • no, i have to return the shuffled list from the func and it returns none. – Kamal Kant Sharma Mar 13 '21 at 06:43
  • `random.shuffle` doesn't return anything. Therefore, if you set a variable to the output of `random.shuffle`, its value will _always_ be `None`. If you really want to return the shuffled list from the function, return the list, not the result from `random.shuffle` (e.g. `return list_` in your function). As I said, this does change the original list, so you should make a copy of it if you want to keep the unshuffled list for further use. – luuk Mar 13 '21 at 17:42