Randomly select elements from a list in Python
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:
|
1 2 3 4 5 |
import random nums = [1, 2, 3, 4, 5] print(random.choice(nums)) |
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.
|
1 2 3 4 5 |
import random nums = [1, 2, 3, 4, 5] print(random.SystemRandom().choice(nums)) |
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:
|
1 2 3 4 5 6 |
import random nums = [1, 2, 3, 4, 5] k = 2 print(random.SystemRandom().sample(nums, k)) |
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:
|
1 2 3 4 5 |
import random nums = [1, 2, 3, 4, 5] print(random.choices(nums, k=2)) |
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:
|
1 2 3 4 5 6 |
import random nums = [1, 2, 3, 4, 5] random.shuffle(nums) print(nums) |
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:
|
1 2 3 4 5 6 |
import random nums = [1, 2, 3, 4, 5] rIndex = random.randrange(len(nums)) print(nums[rIndex]) |
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.
|
1 2 3 4 5 |
import secrets nums = [1, 2, 3, 4, 5] print(secrets.choice(nums)) |
To get the highest-quality sources provided by the operating system, we can use the secrets.SystemRandom class instead.
|
1 2 3 4 5 |
import secrets nums = [1, 2, 3, 4, 5] print(secrets.SystemRandom().choice(nums)) |
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:
|
1 2 3 4 5 6 |
import secrets nums = [1, 2, 3, 4, 5] k = 2 print(secrets.SystemRandom().sample(nums, k)) |
That’s all about randomly select elements from a list in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)