How to Choose Random Elements From a List in Python — CODEFATHER

Claudio Sabato
3 min readNov 26, 2023

--

A common task in Python is choosing random elements from a list. This can be useful in many scenarios like data sampling, game development, or day-to-day scripts.

With this tutorial, you will learn several ways to randomly select elements from a list in Python.

Select Random Elements From a Python List

Python’s random module, provides functions that allow to retrieve random elements from a list.

Let’s compare the behavior of the following functions when you work with a list of characters:

  • random.choice()
  • random.choices()
  • random.sample()

You will pass a list of characters to each one of the three functions to see what they return back.

Before executing examples of code, open the Python shell and import the random module:

>>> import random

Let’s start with the first function!

random.choice()

Using the random.choice() function is the simplest way to choose a random element from a list. This function is designed to return a random element from a sequence (a list is a sequence).

>>> random.choice(['h', 'e', 'l', 'l', 'o'])
'o'
>>> random.choice(['h', 'e', 'l', 'l', 'o'])
'h'
>>> random.choice(['h', 'e', 'l', 'l', 'o'])
'o'

From the example of code above, you have seen how to select a single random element from a list.

random.choices()

If you want to choose multiple random elements from a Python list, you can use the random.choices() function. This function returns a list of random elements from a sequence. The default size of the list returned is 1.

>>> random.choices(['h', 'e', 'l', 'l', 'o'])
['l']
>>> random.choices(['h', 'e', 'l', 'l', 'o'])
['o']
>>> random.choices(['h', 'e', 'l', 'l', 'o'])
['o']

Here is how you can obtain lists with multiple random elements. You have to pass the argument k.

>>> random.choices(['h', 'e', 'l', 'l', 'o'], k=2)
['o', 'l']
>>> random.choices(['h', 'e', 'l', 'l', 'o'], k=2)
['o', 'h']
>>> random.choices(['h', 'e', 'l', 'l', 'o'], k=2)
['l', 'l']

You can see that after passing k=2 you get back a list with two random characters.

Also notice from the last example above, that the random.choices() function allows for duplicate elements to be selected.

random.sample()

If you want to select multiple elements from a list, you can also use the random.sample() function. This function returns k random elements from a sequence.

When you use the function sample() by simply passing the list to it, you get the following error:

>>> random.sample(['h', 'e', 'l', 'l', 'o'])
Traceback (most recent call last):
File "", line 1, in
TypeError: sample() missing 1 required positional argument: 'k'

That’s because sample() requires the k argument to be passed to it.

>>> random.sample(['h', 'e', 'l', 'l', 'o'], k=1)
['l']
>>> random.sample(['h', 'e', 'l', 'l', 'o'], k=2)
['o', 'l']
>>> random.sample(['h', 'e', 'l', 'l', 'o'], k=3)
['h', 'l', 'o']

Conclusion

Selecting random elements from a list is a common operation in Python programming. You have seen that the random module provides various ways to do this.

For more in-depth understanding, you can explore the Python documentation about the random module.

Originally published at https://codefather.tech on November 26, 2023.

--

--

Claudio Sabato
Claudio Sabato

Written by Claudio Sabato

Claudio Sabato is an IT expert with over 15 years of professional experience in Python/Bash programming, Linux Systems Administration and IT Systems Design.