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
Top answer
1 of 16
3540

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
306

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)
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-select-random-value-from-a-list
Select random value from a list-Python - GeeksforGeeks
July 11, 2025 - numpy.random.choice() designed for selecting random elements from arrays or lists with advanced features, such as weighted probabilities.
Discussions

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
March 16, 2023
Removing random element from list
= assigns == compares That said, never use == true or == false on a boolean. The comparison is redundant. A boolean is either true, in which case, the result of comparing with true will be true, or false in which case the result of comparing with true will be false, i.e. the exact state that the boolean already holds. For comparison with false it is the inverted "not" state. Next time be more elaborate. "Doesn't work" is not a problem description. Describe how the code fails. Don't expect us to trace the code for you. Tell us everything you know and have found out. More on reddit.com
🌐 r/learnprogramming
10
0
November 10, 2021
What's the best way to get a random item from an array?

While this works in Bash, you should be aware that it won’t select all items equally likely. For example, suppose you have an array with three elements, and $RANDOM is one of 0, 1, 2, or 3, each equally likely. Then the first element has two chances of being selected (0 and 3) while the second and third element have only one chance. That is, the probabilities are distributed .5 .25 .25 instead of .33… .33… .33….

More on reddit.com
🌐 r/bash
5
2
May 19, 2014
🌐
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 - Note: The choice() function returns a random element from the non-empty sequence. (A sequence can be a list, string, or tuple.) If the list or sequence is empty will raise IndexError (cannot choose from an empty sequence).
🌐
Baeldung
baeldung.com › home › java › java list › java – get random item/element from a list
Java - Get Random Item/Element From a List | Baeldung
April 4, 2025 - It is quite straightforward: public void givenList_whenNumberElementsChosen_shouldReturnRandomElementsRepeat() { Random rand = new Random(); List<String> givenList = Arrays.asList("one", "two", "three", "four"); int numberOfElements = 2; for (int i = 0; i < numberOfElements; i++) { int randomIndex = rand.nextInt(givenList.size()); String randomElement = givenList....
🌐
Better Stack
betterstack.com › community › questions › python-how-to-randomly-select-list-item
How can I randomly select an item from a list in Python? | Better Stack Community
May 14, 2025 - To randomly select an item from a list in Python, you can use the random.choice() function from the random module. This function takes a list as an argument and returns a randomly selected element from the list.
🌐
datagy
datagy.io › home › python posts › python: select random element from a list
Python: Select Random Element from a List • datagy
December 19, 2022 - It also provides a seed generator ... to use Python to select a single random element from a list in Python is to use the random.choice() function....
🌐
Rosetta Code
rosettacode.org › wiki › Pick_random_element
Pick random element - Rosetta Code
1 week ago - The unary operator '?' selects a random element from its argument which may be a string, list, table, or set. procedure main() L := [1,2,3] # a list x := ?L # random element end ... import java.util.Random; ... int[] array = {1,2,3}; return ...
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › is there a single command to randomly select an item from a list and then delete it?
r/learnpython on Reddit: Is there a single command to randomly select an item from a list and then delete it?
March 31, 2018 -

Hi, I searched but didn't find anything.

Basically, I want something which combines random.choice() and list.pop() - the idea being that the command will randomly choose an item from the list, return it, and delete it.

The use case is that I have a list of items that I want to process in a random order; once they are processed they will be removed from the list so that they can't be randomly selected again. This continues until the list is empty.

I've implemented this with a combination of random.choice() and pop() as follows:

foo = ['a','b','c','d','e','f']
    while len(foo) > 0:
        random_index = random.randint(0,len(foo) - 1)
        randomly_selected_item = foo.pop(random_index)

It feels like the last two lines could be replaced with random.remove_random_item(list) if such a thing existed.

🌐
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 - In Python, you can randomly sample elements from a list using the choice(), sample(), and choices() functions from the random module. These functions also work with strings and tuples. choice() return ...
🌐
Codingem
codingem.com › home › get random element from a python list—random.choice()
Get Random Element from a Python List—random.choice()
July 10, 2025 - Pick a random integer index between 0 and the length of the list – 1. Use the randomized index to grab the corresponding element from a list.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › select random item from list in python
Select Random Item from List in Python - Spark By {Examples}
May 31, 2024 - 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.
🌐
Vultr
docs.vultr.com › python › examples › randomly-select-an-element-from-the-list
Python Program to Randomly Select an Element From the List | Vultr Docs
December 27, 2024 - Use random.sample() and specify the list and the number of items. ... This code snippet selects three unique fruits from the list fruits. Each time you execute this code, it will output a different combination of three fruits, none of which ...
🌐
Sololearn
sololearn.com › en › Discuss › 2237407 › how-to-get-random-element-from-a-list-in-c-code
How to get random element from a list in c code? | Sololearn: Learn to code for FREE!
I wanted to know if there is a way to get a random item from a list or a random integer from a given range... ... Atiksh Genius , Read this article https://www.geeksforgeeks.org/rand-and-srand-in-ccpp/amp/ Generate a random number and use it as index to access element from array.
🌐
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 - import random random.seed(0) list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] random.shuffle(list) random_items = list[:4] print(random_items) ... The random.sample() function can be used to choose k unique random elements from a population sequence.
🌐
Programiz
programiz.com › python-programming › examples › random-list-element
Python Program to Randomly Select an Element From the List
import secrets my_list = [1, 'a', 32, 'c', 'd', 31] print(secrets.choice(my_list)) ... Using choice() method of secrets module, you can select a random element from the list.
🌐
GeeksforGeeks
geeksforgeeks.org › randomly-select-n-elements-from-list-in-python
Randomly Select N Elements from List in Python - GeeksforGeeks
January 28, 2025 - The list comprehension selects n random elements based on these indices. This method also allows repetition of elements. ... 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.
🌐
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 - To implement this approach, let's look at some methods to generate random numbers in Python: random.randint() and random.randrange(). We can additionally use random.choice() and supply an iterable - which results in a random element from that iterable being returned back. random.randint(a, b) ...
🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch02s09.html
Selecting Random Elements from a List Without Repetition - Python Cookbook [Book]
July 19, 2002 - def faster( ): while data: index = random.randrange(len(data)) elem = data[index] # direct deletion, no search needed del data[index] process(elem) # the same, but preserving the data list def faster_preserve( ): aux = range(len(data)) while aux: posit = random.randrange(len(aux)) index = aux[posit] elem = data[index] # alters the auxiliary list only del aux[posit] process(elem) However, the key improvement is to switch to an O(N) algorithm: def improved( ): size = len(data) while size: index = random.randrange(size) elem = data[index] data[index] = data[size-1] size = size - 1 process(elem)
Authors   Alex MartelliDavid Ascher
Published   2002
Pages   608
🌐
PythonForBeginners.com
pythonforbeginners.com › home › select random element from a list in python
Select Random Element from A List in Python - PythonForBeginners.com
June 29, 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.