Shuffle an array with python, randomize array item order with python
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping
--
Chapters
00:00 Shuffle An Array With Python, Randomize Array Item Order With Python
00:13 Accepted Answer Score 634
00:20 Answer 2 Score 118
00:27 Answer 3 Score 21
00:51 Answer 4 Score 31
00:59 Thank you
--
Full question
https://stackoverflow.com/questions/4739...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #arrays #random #shuffle
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping
--
Chapters
00:00 Shuffle An Array With Python, Randomize Array Item Order With Python
00:13 Accepted Answer Score 634
00:20 Answer 2 Score 118
00:27 Answer 3 Score 21
00:51 Answer 4 Score 31
00:59 Thank you
--
Full question
https://stackoverflow.com/questions/4739...
--
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']):