In this post, we will explore different ways to randomly select elements from a list in Python.

1. Using random module

The random module is an in-built module is used to generate random numbers and sequences. The random module provides several methods that can help us randomly select elements from a list, such as:

1. Using random.choice function

The standard solution to return a random element from a list is to use the random.choice() function. This function takes a sequence (such as a list) as an argument, and returns a single random element from it. For example:

Download  Run Code

 
The random.choice() function returns only a single element at a time, and it may return the same element when called again. For cryptographically secure random elements, we can use the choice() function from random.SystemRandom class, which generates random numbers from sources provided by the OS.

Download  Run Code

2. Using random.sample function

If we need to select more than one element from a sequence randomly, we can use random.sample() function instead. It takes two arguments: a population (such as a list or a set) and a sample size k, and returns a list of k unique random elements from the population. The sample size must be less than or equal to the population size. For example:

Download  Run Code

3. Using random.choices() function

A third way to randomly select elements from a list is to use the random.choices() function. This function takes a population, optional weights or cumulative weights for each element, and an optional sample size, and returns a list of random elements from the population. The sample size defaults to one if not specified. Here is an example of its usage:

Download  Run Code

 
The advantage of this method is that it can return multiple elements at once, and allows us to specify weights for each element. However, it may return duplicates in the sample.

4. Using random.shuffle() function

A fourth way to randomly select elements from a list is to use the random.shuffle() function. This function takes a sequence as an argument and optionally a custom random function, and randomly reorders the elements in-place. The elements can be accessed by indexing or slicing the sequence. Here is an example:

Download  Run Code

 
Note that this method modifies the entire sequence in-place, which may not be desirable in some cases.

5. Using random.randrange() function

Finally if we need the index of the random element, we can use the random.randrange function. Here is an example of how to use this function:

Download  Run Code

2. Using secrets module

As of Python 3.6, we should use the secrets module for security or cryptographic uses over the pseudorandom generators from the random module. The secrets module has a few methods that can help us randomly select elements from a list, such as:

1. Using secrets.choice function

We can use secrets.choice() function to get a randomly chosen element from a list. It takes a non-empty sequence (such as a list), and returns a randomly chosen element from it.

Download  Run Code

 
To get the highest-quality sources provided by the operating system, we can use the secrets.SystemRandom class instead.

Download  Run Code

2. Using secrets.sample() function

If we need to randomly select multiple elements from a list, we can use the sample() function from secrets.SystemRandom class. It takes two arguments: a population and a sample size k, where k must be less than or equal to the population size. For example:

Download  Run Code

That’s all about randomly select elements from a list in Python.