I am trying to shuffle a list of tuples in Python,
import random
a = [(1,2,3),(4,5,6),(7,8,9)]
b = random.shuffle(a)
but when I run the above, b
is None
.
How can I shuffle a list of tuples?
I am trying to shuffle a list of tuples in Python,
import random
a = [(1,2,3),(4,5,6),(7,8,9)]
b = random.shuffle(a)
but when I run the above, b
is None
.
How can I shuffle a list of tuples?
To shuffle a but return the shuffled list and leave a
intact use random.sample
import random
a = [(1,2,3),(4,5,6),(7,8,9)]
b = random.sample(a, k=len(a))
print(b)
#[(4, 5, 6), (7, 8, 9), (1, 2, 3)]
print(a)
#[(1,2,3),(4,5,6),(7,8,9)]
random.shuffle
shuffles a list in place so the return value is None
.
import random
a = [(1,2,3),(4,5,6),(7,8,9)]
random.shuffle(a)
print(a)
#[(4, 5, 6), (7, 8, 9), (1, 2, 3)]