Shuffle an array with python, randomize array item order with python
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Music Box Puzzles
--
Chapters
00:00 Question
00:20 Accepted answer (Score 600)
00:29 Answer 2 (Score 114)
00:38 Answer 3 (Score 51)
01:12 Answer 4 (Score 22)
01:27 Thank you
--
Full question
https://stackoverflow.com/questions/4739...
Answer 2 links:
[sklearn]: http://scikit-learn.org/stable/modules/g...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #arrays #random #shuffle
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Music Box Puzzles
--
Chapters
00:00 Question
00:20 Accepted answer (Score 600)
00:29 Answer 2 (Score 114)
00:38 Answer 3 (Score 51)
01:12 Answer 4 (Score 22)
01:27 Thank you
--
Full question
https://stackoverflow.com/questions/4739...
Answer 2 links:
[sklearn]: http://scikit-learn.org/stable/modules/g...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #arrays #random #shuffle
#avk47
ACCEPTED ANSWER
Score 634
import random
random.shuffle(array)
ANSWER 2
Score 118
import random
random.shuffle(array)
ANSWER 3
Score 31
Just in case you want a new array you can use sample:
import random
new_array = random.sample( array, len(array) )
ANSWER 4
Score 21
The other answers are the easiest, however it's a bit annoying that the random.shuffle method doesn't actually return anything - it just sorts the given list. If you want to chain calls or just be able to declare a shuffled array in one line you can do:
import random
def my_shuffle(array):
random.shuffle(array)
return array
Then you can do lines like:
for suit in my_shuffle(['hearts', 'spades', 'clubs', 'diamonds']):