Shuffle elements in a list in Python

Money and Business

If you want to shuffle (randomly sort) the elements of a list (array) in Python, use the random module of the standard library.

There are two functions: shuffle(), which randomly sorts the original list, and sample(), which returns a new randomly sorted list. sample() can be used for strings and tuples.

  • Shuffle the original list:random.shuffle()
  • Generate a new, shuffled list.:random.sample()
  • Shuffle strings and tuples
  • Fix the random number seed

If you want to sort in ascending or descending order instead of random, or in reverse order, see the following article.

Shuffle the original list: random.shuffle()

The function shuffle() in the random module can randomly sort the original list.

import random

l = list(range(5))
print(l)
# [0, 1, 2, 3, 4]

random.shuffle(l)
print(l)
# [1, 0, 4, 3, 2]

Generate a new, shuffled list.: random.sample()

The function sample() in the random module can be used to create a new list that is randomly sorted, without changing the original list.

sample() is a function that randomly selects and retrieves an element from a list. The first argument is a list, and the second argument is the number of elements to be retrieved. See the following article for details.

If the number of elements to be selected by sample() is the total number of elements in the list, we get a new list with all elements randomly sorted. The total number of elements in the list is obtained by len().

The original object will not be changed.

l = list(range(5))
print(l)
# [0, 1, 2, 3, 4]

lr = random.sample(l, len(l))
print(lr)
# [0, 3, 1, 4, 2]

print(l)
# [0, 1, 2, 3, 4]

Shuffle strings and tuples

Strings and tuples are immutable, so if you use random.shuffle() to change the original object, you will get the following error.
TypeError

s = 'abcde'

# random.shuffle(s)
# TypeError: 'str' object does not support item assignment

t = tuple(range(5))
print(t)
# (0, 1, 2, 3, 4)

# random.shuffle(t)
# TypeError: 'tuple' object does not support item assignment

If you want to shuffle a string or tuple, use random.sample(), which creates a new object.

Even when a string or tuple is specified as an argument, random.sample() returns a list, so it is necessary to process it back to a string or tuple.

In the case of a string, it will be a list of characters one by one. To concatenate them into a single string again, use the join() method.

sr = ''.join(random.sample(s, len(s)))
print(sr)
# bedca

For tuples, use tuple(), which creates a tuple from a list.

tr = tuple(random.sample(t, len(l)))
print(tr)
# (0, 1, 2, 4, 3)

Fix the random number seed

By giving an arbitrary integer to the random module's function seed(), the random number seed can be fixed and the random number generator can be initialized.

After initialization with the same seed, it is always reordered in the same way.

l = list(range(5))
random.seed(0)
random.shuffle(l)
print(l)
# [2, 1, 0, 4, 3]

l = list(range(5))
random.seed(0)
random.shuffle(l)
print(l)
# [2, 1, 0, 4, 3]
Copied title and URL