
Python List Shuffle
You can shuffle Python list by importing random
and using shuffle
.
import random
a = [1, 2, 3]
random.shuffle(a)
print(a) # [2, 1, 3]
shuffle()
updates the original list and returns None.
import random
a = [1, 2, 3]
b = random.shuffle(a)
print(b) # None
Random seed
You can set the seed by random.seed() globally.
import random
random.seed(7)
a = [1, 2, 3]
random.shuffle(a)
print(a) # [3, 1, 2]
The sorted list is always same whenever running the program. If you don't set the seed, the result changes every running.
Comments
Powered by Markdown