Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant Get a result form random.shuffle in python

Tags:

python

I have never had this problem before but when i try and shuffle a list i get a return of 'None'

import random
c=[1,4,67,3]
c=random.shuffle(c)
print c

The print statement returns 'None' and i dont know why, I have looked around for an answer to this problem but there doesent seem to be anything. I hope i am not making an obvious mistake.

like image 658
Koreus7 Avatar asked Dec 09 '22 10:12

Koreus7


1 Answers

The random.shuffle function sorts the list in-place, and to avoid causing confusion on that point, it doesn't return the shuffled list. Try just:

 random.shuffle(c)
 print(c)

This is a nice bit of API design, I think - it means that if you misunderstand what random.shuffle is doing, then you'll get a obvious problem immediately, rather than a more subtle bug that's difficult to track down later on...

like image 173
Mark Longair Avatar answered Dec 30 '22 17:12

Mark Longair