Use random.choice():

import random

foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))

For cryptographically secure random choices (e.g., for generating a passphrase from a wordlist), use secrets.choice():

import secrets

foo = ['battery', 'correct', 'horse', 'staple']
print(secrets.choice(foo))

secrets is new in Python 3.6. On older versions of Python you can use the random.SystemRandom class:

import random

secure_random = random.SystemRandom()
print(secure_random.choice(foo))
Answer from Pฤ“teris Caune on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ randomly-select-n-elements-from-list-in-python
Randomly Select N Elements from List in Python - GeeksforGeeks
July 15, 2025 - When working with lists in Python, we often need to randomly select a specific number of elements. For example, consider the list a = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]. We might want to randomly select 3 elements from this list.
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ random โ€บ python random choice() function to select a random item from a list and set
Python random choice() function to select a random item from a List and Set
July 22, 2023 - Use random.choice() function to randomly select an item from a list, String, Dictionary, and set. Pick a single random number from a range
Discussions

python - Select 50 items from list at random - Stack Overflow
I have a function which reads a list of items from a file. How can I select only 50 items from the list randomly to write to another file? def randomizer(input, output='random.txt'): query = open( More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to get randomly select n elements from a list using in numpy? - Stack Overflow
0 Randomly selecting an element (float numbers, not integers) from an array python? 3 Create numpy array with random elements from list More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is there a single command to randomly select an item from a list and then delete it?
You can random.shuffle() the list and just iterate over it. It would produce the same effect. More on reddit.com
๐ŸŒ r/learnpython
8
19
March 31, 2018
Optimal way for retrieving a random element from a set?
https://stackoverflow.com/questions/15993447/python-data-structure-for-efficient-add-remove-and-random-choice More on reddit.com
๐ŸŒ r/learnpython
15
4
October 8, 2023
People also ask

How do I select one random item from a Python list?
Use random.choice on a non-empty sequence; it returns one element and raises IndexError for an empty sequence.
๐ŸŒ
pythonpool.com
pythonpool.com โ€บ home โ€บ tutorials โ€บ randomly select from a list in python: choice, sample, and choices
Randomly Select From a List in Python: choice, sample, and choices
Should I use random or secrets for security?
Use secrets.choice for security-sensitive decisions such as tokens or recovery choices; random is for ordinary simulation and application behavior.
๐ŸŒ
pythonpool.com
pythonpool.com โ€บ home โ€บ tutorials โ€บ randomly select from a list in python: choice, sample, and choices
Randomly Select From a List in Python: choice, sample, and choices
How do I select items with replacement?
Use random.choices when repeated selections are allowed or when weights should influence the result.
๐ŸŒ
pythonpool.com
pythonpool.com โ€บ home โ€บ tutorials โ€บ randomly select from a list in python: choice, sample, and choices
Randomly Select From a List in Python: choice, sample, and choices
Top answer
1 of 16
3547

Use random.choice():

import random

foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))

For cryptographically secure random choices (e.g., for generating a passphrase from a wordlist), use secrets.choice():

import secrets

foo = ['battery', 'correct', 'horse', 'staple']
print(secrets.choice(foo))

secrets is new in Python 3.6. On older versions of Python you can use the random.SystemRandom class:

import random

secure_random = random.SystemRandom()
print(secure_random.choice(foo))
2 of 16
307

If you want to randomly select more than one item from a list, or select an item from a set, I'd recommend using random.sample instead.

import random
group_of_items = {'a', 'b', 'c', 'd', 'e'}  # a sequence or set will work here.
num_to_select = 2                           # set the number to select here.
list_of_random_items = random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]
second_random_item = list_of_random_items[1] 

If you're only pulling a single item from a list though, choice is less clunky, as using sample would have the syntax random.sample(some_list, 1)[0] instead of random.choice(some_list).

Unfortunately though, choice only works for a single output from sequences (such as lists or tuples). Though random.choice(tuple(some_set)) may be an option for getting a single item from a set.

EDIT: Using Secrets

As many have pointed out, if you require more secure pseudorandom samples, you should use the secrets module:

import secrets                              # imports secure module.
secure_random = secrets.SystemRandom()      # creates a secure random object.
group_of_items = {'a', 'b', 'c', 'd', 'e'}  # a sequence or set will work here.
num_to_select = 2                           # set the number to select here.
list_of_random_items = secure_random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]
second_random_item = list_of_random_items[1]

EDIT: Pythonic One-Liner

If you want a more pythonic one-liner for selecting multiple items, you can use unpacking.

import random
first_random_item, second_random_item = random.sample({'a', 'b', 'c', 'd', 'e'}, 2)
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ how-to-randomly-select-elements-from-a-list-in-python
How to Randomly Select Elements from a List in Python
September 27, 2023 - import random def select_random_Ns(lst, n): random.shuffle(lst) result = [] for i in range(0, len(lst), n): result.append(lst[i:i + n]) return result lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] print(select_random_Ns(lst, 2)) This results in a list of random pairs, without repetition: ... In this article, ...
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Random Sampling from a List in Python: random.choice, sample, choices | note.nkmk.me
May 19, 2025 - Concatenate Strings in Python: + Operator, join, etc. print(tuple(random.sample(('xxx', 'yyy', 'zzz'), 2))) # ('zzz', 'yyy') print(''.join(random.sample('abcde', 2))) # be ... Note that if the original list or tuple contains duplicate elements, the same values may be selected.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-select-random-value-from-a-list
Select random value from a list-Python - GeeksforGeeks
July 11, 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. random.choice() picks an element at random without any need for indexing, making it ideal for quick selections.
Find elsewhere
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ random.html
random โ€” Generate pseudo-random numbers
This module implements pseudo-random number generators for various distributions. For integers, there is uniform selection from a range. For sequences, there is uniform selection of a random element, a function to generate a random permutation of a list in-place, and a function for random sampling without replacement.
๐ŸŒ
py4u
py4u.org โ€บ blog โ€บ randomly-select-n-elements-from-list-in-python
Randomly Select N Elements from a List in Python
population: The list (or iterable) to sample from. weights/cum_weights: Optional weights for weighted sampling (e.g., biased dice rolls). k: The number of elements to select (can be larger than the length of population). import random my_list = [1, 2, 3] # Select 5 elements (with possible repeats) choices = random.choices(my_list, k=5) print(choices) # Example output: [2, 3, 2, 1, 3]
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python: select random element from a list
Python: Select Random Element from a List โ€ข datagy
December 19, 2022 - To use Python to select random elements without replacement, we can use the random.sample() function. The function accepts two parameters: the list to sample from and the number of items to sample.
๐ŸŒ
Python Pool
pythonpool.com โ€บ home โ€บ tutorials โ€บ randomly select from a list in python: choice, sample, and choices
Randomly Select From a List in Python: choice, sample, and choices
July 13, 2026 - For valid RGB, hexadecimal, and named-color generation rather than arbitrary list items, continue with Generate Random Colors in Python. Check that the list has items before calling choice(). This avoids an IndexError for empty input. import random names = [] if names: selected = random.choice(names) else: selected = "No names available" print(selected)
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ select-n-random-items-from-list-python
Selecting `n` Random Items from a List in Python - CodeRivers
February 22, 2026 - In this example, we import the random module, define a list my_list, and specify the number of items n we want to select. The random.sample() function returns a new list containing n unique random items from my_list.
๐ŸŒ
Newtum
blog.newtum.com โ€บ randomly-select-element-from-list-in-python-using-choices
Randomly Select Element From List in Python Using choices() - Newtum
April 24, 2024 - In conclusion, the Python code ... random.choices() function to randomly select element from a list. By importing the random module and declaring a list, we can generate a random selection of elements. The code allows us to specify the number of elements to be selected by initializing the variable n...
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ random-sample-in-python
Python Random Sample from List - Complete Guide 2026
June 16, 2026 - It also supports weighted sampling, where some elements have a higher probability of being selected than others. numpy.random.choice() is the NumPy equivalent for random sampling and is the preferred tool in data science contexts when working with NumPy arrays. It supports both with and without replacement, weighted sampling, and works efficiently with large datasets. In NumPy 1.17 and later, the recommended approach is to use the Generator API: rng = numpy.random.default_rng(seed=42) then rng.choice(population, size=k, replace=False).
๐ŸŒ
PythonForBeginners.com
pythonforbeginners.com โ€บ home โ€บ select random element from a list in python
Select Random Element from A List in Python - PythonForBeginners.com
February 24, 2022 - For this, we will use the โ€œsizeโ€ parameter of the function. If we want to select n random elements from a given list, we will pass the number n as the second input argument to the choice() function defined in the numpy module.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ randomly-select-elements-from-list-without-repetition-in-python
Randomly select elements from list without repetition in Python - GeeksforGeeks
July 15, 2025 - random.shuffle() function randomly rearranges the list, and we can take the first n elements as the selection. ... import random a = [10, 20, 30, 40, 50] # Shuffling the list random.shuffle(a) # Selecting first 3 elements res = a[:3] print(res) ...
๐ŸŒ
Sling Academy
slingacademy.com โ€บ article โ€บ python-ways-to-select-random-elements-from-a-list
Python: 3 Ways to Select Random Elements from a List - Sling Academy
July 6, 2023 - In case you only need to get a single random element from a list, you can use the random.choice() function: