Python Function Reference

random.shuffle

Shuffle the items of a list into a random order.

Python 3.13
random.shuffle(x)

Parameters

xlist
The list to shuffle.

Return value

Nothing (None); the list is changed in place.

Examples

Shuffling a deck

Python 3.13
import random

random.seed(1)
cards = ['A', 'K', 'Q', 'J']
random.shuffle(cards)
print(cards)
['J', 'A', 'Q', 'K']

A shuffled copy with random.sample

Python 3.13
import random

random.seed(3)
names = ['Anna', 'Boris', 'Vera']
shuffled = random.sample(names, k=len(names))
print(names)
print(shuffled)
['Anna', 'Boris', 'Vera']
['Anna', 'Vera', 'Boris']

Details

The list is shuffled in place, and the function returns None: cards = random.shuffle(cards) loses the list. Strings and tuples cannot be shuffled because they are immutable; random.sample(x, k=len(x)) gives a shuffled copy of any sequence.

See also

Where to learn