How can I recreate Ruby's sort_by {rand} in python?

Question:

I have a line of code where can sort an array into random order:

someArray.sort_by {rand}

so in python, how can I convert to it to python code?

Asked By: hanan

||

Answers:

Maybe you are looking for this:

import random
l = [1, 2, 3]
# l is shuffled in place
random.shuffle(l)
# Print to see the shuffled l
print(l)
Answered By: Aidis

You can reach this by:

import random
array = [1, 2, 3]

print(array) #now nothing is changed because shuffle was not applied
random.shuffle(array)
print(array) #then now array is randomized.

hope it helps.

Answered By: Gipraltar