Skip to content

Randomly Select an Item from List in Python AlixaProDev Spark By {Examples}

  • by

Need to randomly select an item from the list? 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().

1. Quick Examples of Randomly Select an Item from the List

These examples will give a quick overview of some of the different methods to randomly select an item from a list in Python. 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
random.shuffle(my_list)
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. random.choice() – Select Random Item from List

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.

import random

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

print(“Selected:”, random_choice)

3. random.sample() – Random Element from List

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

import random

random_list = random.sample(my_list , 1)
# Output
# [5]

Below are the important point 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. secrets.choice() – Select Random Choice from List

The secrets.choice() function is also Python built-in function that is designed for generating cryptographically secure random numbers. You can aslo 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. numpy.random.choice() – Random Number with Numpy

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 funciton in advanced application, the complete syntax of the function is below:

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.

import random

random.shuffle(my_list)

print(“Shuffled list:”, my_list)
# Output:
# Shuffled list: [5, 2, 1, 3, 4]

7. Summary and Conclusion

We have explained several ways to randomly select an item 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!

 Need to randomly select an item from the list? 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().  Read More Python, Python Tutorial, Python List Examples 

Leave a Reply

Your email address will not be published. Required fields are marked *