all Python 3randomterm
random.shuffle
shuffling
Shuffles a list in place at random. It changes the original object and returns nothing.
Syntax
random.shuffle(x)
The classic slip is assigning the result: deck = random.shuffle(deck) replaces the list with None. Only mutable sequences work — a tuple or a string raises TypeError; to get a shuffled copy while keeping the original intact, use random.sample(x, len(x)).
Examples
import random
random.seed(10)
deck = list(range(1, 14))
random.shuffle(deck)
print(deck) # → перемешанная колода
words = ['alpha','beta','gamma','delta']
random.shuffle(words)
print(words) # → случайный порядок
# Копия перемешанная:
import random
original = [1,2,3,4,5]
import copy; shuffled = copy.copy(original)
random.shuffle(shuffled)
print(original, shuffled)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy