How to randomly select an item from a list in Python?

How to randomly select an item from a list in Python?

You can use a method like random.choice(), random.randint(), random.sample(), random.choice(), random.random() in Python, to randomly select an item from a list.

A list is used to store multiple values in a single variable. The list may contain mixed 

types of data, and in this article, we will explore how to select random items from a list.

Table of Contents:

Methods to Randomly Select an Item from a List in Python

Below are a few methods to randomly select an item from a list in Python:

Method 1: Using random.choice() in Python

This method is used to return the random element from a list, tuple, or string, and it is an inbuilt function.

Example:

Python

Output:

Explanation: You get the random elements from the list using the random.choice() module.

Method 2: Using random.randrange() in Python

Let’s say you have some range of list length, and this function is used to generate the random index out of it. And we use this random index to get that element.

Example: 

Python

Output: 

Explanation: The random.randrange() is used in this code, to generate the random index. And you will get the item using the generated index. 

Method 3: Using random.random() function in Python

The floating-point numbers produced by this method fall between 0 and 1. t doesn’t need any input and gives random numbers between 0 and 1, all with equal chances.

Example:

Python

Output:

Explanation: This Python code selects a random element from the list_a by generating a random index. It then prints the selected element.

Method 4: Using random.sample() in Python

Using this function, it is easy to choose multiple items from a list or just one item from a specific sequence.

Example:

Python

Output: 

Explanation: This Python code creates a list of programming languages and frameworks. It then prints a randomly shuffled list of all five elements in list_a.

Method 5: Using random.randint() in Python

It works the same as the random.randrange() function, it only differs by having two mandatory arguments. Other than that, it generates a random number in the given range.

Example: 

Python

Output: 

Explanation: This Python code generates a random index within the range of the list_a elements. It then accesses and prints the element at the randomly generated index.

Method 6: Using random.choices() in Python

This method randomly selects the multiple elements and also supports the repetition of the element to get returned.

Example: 

Python

Output: 

Explanation: This Python code selects three random elements from the list list_a, allowing repetition.

Method 7: Select k random value from a list

To select a random value, a random.sample() can be used. This function allows you to choose more items without repeating them.

Example:

Python

Output:

Explanation: The code randomly selects 3 elements from a list, then checks if the list length is divisible by 3 and groups the elements accordingly. If it’s not divisible, it notifies that proper pairing isn’t possible.

Method 8: Using random.shuffle() in Python

The random.shuffle() modifies the list in place and we take the first n elements after shuffling.

Example: 

Python

Output: 

Explanation: Your code shuffles a list of elements and then selects the first three elements from the shuffled list. 

Method 9: Using secret.choice() in Python  

It is a more secure way to randomly select an item from a list in Python. This is used to avoid predictable randomness. 

Example:

Python

Output:

Explanation: The code initializes a list of strings and then uses the secrets module to randomly and securely select one item from the list. Finally, it prints the randomly selected item.

Performance Comparison of All Methods

MethodsPerformanceProsCons
random.choice()O(1)Fast and simple,No support for multiple selections
random.randrange()O(1)Flexible for range-based selectionRequires indexing manually
random.random()O(1)Flexible, can be combined for scalingMore complex, requires typecasting
random.sample()O(k)Great for non-repeating samplesInefficient for single selections
random.randint()O(1)Simple to use for range-based indexingRequires manual indexing
random.choices()O(k)Supports repetition Less efficient for non-repeated items
random.shuffle()O(n)Great for list reorderingModifies the list, computationally expensive
secrets.choice()O(1)Cryptographically secureSlightly slower, overkill for non-secure tasks

Conclusion 

The random.choice() is a built-in function, and this function is used to return randomly. The random.randrange() function is used to return random elements based on their index. The random.randint() function resembles the random.randrange() function and random.choices() function returns the random element with repetition. The choice of method is based on your requirements.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner