Select random value from a list-Python
Last Updated :
11 Jul, 2025
The goal here is to randomly select a value from a list in Python. For example, given a list [1, 4, 5, 2, 7], we want to retrieve a single randomly chosen element, such as 5. There are several ways to achieve this, each varying in terms of simplicity, efficiency and use case. Let's explore different approaches to accomplish this task.
Using random.choice()
random.choice() picks an element at random without any need for indexing, making it ideal for quick selections. This method is fast and widely used when you need to randomly pick one item from a collection.
Python
import random
a = [1, 4, 5, 2, 7]
res = random.choice(a)
print(res)
Using random.randint()
random.randint() generate a random index within the range of the list’s length, then access the element at that index. This approach gives you more control over the index and allows for indexing-based operations alongside random selection.
Python
import random
a = [1, 4, 5, 2, 7]
idx = random.randint(0, len(a) - 1)
res = a[idx]
print(res)
Using secrets.choice()
secrets.choice() is part of the secrets module, designed for cryptographic applications where security is important. It provides a randomly chosen element from a list using a cryptographically secure method, making it more secure than random.choice().
Python
import secrets
a = [1, 4, 5, 2, 7]
res = secrets.choice(a)
print(res)
Using random.sample()
random.sample() selects a specified number of unique elements from a list, even when k=1 to pick a single item. It’s useful when you need non-repeating selections. Although it's slightly less efficient than choice() for a single item, it’s helpful when you plan to extend the functionality to multiple selections without repetition.
Python
import random
a = [1, 4, 5, 2, 7]
res = random.sample(a, 1)[0]
print(res)
Using numpy.random.choice()
numpy.random.choice() designed for selecting random elements from arrays or lists with advanced features, such as weighted probabilities. It’s more suited for working with large datasets or NumPy arrays and allows for options like sampling with replacement or applying a probability distribution.
Python
import numpy as np
a = [1, 4, 5, 2, 7]
res = np.random.choice(a)
print(res)
Related Articles:
Similar Reads
Select Random Element from Set in Python Selecting a random element from a group of samples is a deterministic task. A computer program can mimic such a simulation of random choices by using a pseudo-random generator. In this article, we will learn how to select a random element from a set in Python. What is a Set in Python?A Set is an uno
3 min read
Remove falsy values from a list in Python Removing falsy values from a list filters out unwanted values like None, False, 0 and empty strings, leaving only truthy values. It's useful for data cleaning and validation.Using List ComprehensionList comprehension is a fast and efficient way to remove falsy values from a list . It filters out val
1 min read
How to Select a Random Element from a Tuple in Python Selecting a random element consists of retrieving one element from a collection in an unpredictable manner. In Python, this can be done easily using different methods provided by the random module. Below are the three different approaches to select a random element from a tuple. Select a Random Elem
2 min read
How to Skip a Value in a List in Python Skipping a value in a list means we want to exclude certain items while iterating. There are many ways to skip a value in Python List. Using continue is the simplest way to skip a value in iteration, without modifying the original list. Pythona = [1, 2, 3, 4, 5] # iterating the list for i in a: if i
2 min read
Select any row from a Dataframe in Pandas | Python In this article, we will learn how to get the rows from a dataframe as a list, without using the functions like ilic[]. There are multiple ways to do get the rows as a list from given dataframe. Letâs see them will the help of examples. Python3 # importing pandas as pd import pandas as pd # Create t
1 min read
Select any row from a Dataframe in Pandas | Python In this article, we will learn how to get the rows from a dataframe as a list, without using the functions like ilic[]. There are multiple ways to do get the rows as a list from given dataframe. Letâs see them will the help of examples. Python3 # importing pandas as pd import pandas as pd # Create t
1 min read