You are currently viewing Select Random Item from List in Python

How to select a random item from the list in Python? There are several methods available in Python to accomplish this task. In this article, we will discuss different approaches to randomly selecting an item from a list in Python. Some of the methods that can be used for selecting random elements include, random.choice(), random.sample(), and secrets.choice() and NumPy.


1. Quick Examples of Selecting Random Items from the List

These examples will give a quick overview of some of the different methods to randomly select an item from a list. We will discuss each of these methods in more detail later on.

# Quick examples of selecting random item from List

# Define a sample list
my_list = [1, 2, 3, 4, 5]

# Example 1 - random.choice() function
import random
random_choice = random.choice(my_list)
print("random.choice():", random_choice)

# Example 2 - random.sample() function
import random
random_sample = random.sample(my_list, k=1)[0]
print("random.sample():", random_sample)

# Example 3 - secrets.choice() function
import secrets
secrets_choice = secrets.choice(my_list)
print("secrets.choice():", secrets_choice)

# Example 4 - random.shuffle() function
shuffle_choice = my_list[0]
print("random.shuffle():", shuffle_choice)

# Example 5 - numpy.random.choice() function
import numpy as np
numpy_choice = np.random.choice(my_list)
print("np.random.choice():", numpy_choice)

2. Select Random Item from List using random.choice()

The random.choice() function is a built-in Python function that can be used to randomly select an item from a list. It takes a list as an argument and returns a randomly chosen element from that list.

The function returns a single item, not a list or other collection. If the list is empty, the function will raise an IndexError.

# Using random.choice()
import random
my_list = [1, 2, 3, 4, 5]
random_choice = random.choice(my_list)

print("Selected:", random_choice)

When I run this, I get the value 5 as a random element from the list.

3. Random Element from List using sample()

The random.sample() function is another built-in Python function, which you can use to randomly select multiple items from a list. It takes two arguments: the list to choose from and the number of items to choose from.

# Using random.sample()
import random
random_list = random.sample(my_list , 2)

# Output
# [2, 5]

Below are the important points about random.sample() that you should keep in mind:

  • The function returns a list of items, not a single item.
  • If the specified number of items to choose is larger than the size of the sequence, the function will raise a ValueError.
  • If the sequence contains duplicate items, they can select more than once.

4. Using secrets.choice()

The secrets.choice() function is also Python built-in function that is designed for generating cryptographically secure random numbers. You can also use it to randomly select an item from a list. However, the secrets module was introduced in Python 3.6 and may not be available in earlier versions of Python.

See the following Example:

import secrets
my_list = [1, 2, 3, 4, 5]
random_choice = secrets.choice(my_list)

print("Random item:", random_choice)

5. Random Number with Numpy Python Module

If your program is already using the numpy module, then you don’t need to use any other module. The numpy.random.choice() function can be used to randomly select an item from a list or array, and it allows you to specify the probability distribution of the items in the list.

import numpy as np
random_choice = np.random.choice(my_list)

print("Random selected item:", random_choice)

You can use this function in advanced applications, the complete syntax of the function is below:

# Syntax
random_choice = np.random.choice(sequence, size=None, replace=True, p=None)

The Parameters of the functions are :

  • The function returns one or more items, depending on the size argument.
  • The sequence argument can be any sequence type, including lists, tuples, and arrays.
  • If replace is True (the default), the function may select the same item multiple times.
  • You can use the p argument to specify a probability distribution for the items in the sequence.

See the function with complete list of paramters:

import numpy as np

probabilities = [0.2, 0.1, 0.3, 0.2, 0.2]
random_choices = np.random.choice(my_list, size=3, replace=False, p=probabilities)

print("selected items:", random_choices)

6. random.shuffle() – Shuffle the List

The random.shuffle() function in Python shuffles the elements of a list randomly, so that each possible ordering is equally likely. This function modifies the original list in place and returns None.

# random.shuffle()
import random
print("Shuffled list:", my_list)

# Output:
# Shuffled list: [5, 2, 1, 3, 4]

7. Summary and Conclusion

We have explained several ways to select a random item or multiple items from a list in Python. You have learned the built-in random module and its various functions like random.choice(), random.sample(), and random.shuffle(), as well as the use of third-party libraries like NumPy. If you have any questions, please leave a comment below.

Happy coding!