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 OverflowUse 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))
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)
Is there a single command to randomly select an item from a list and then delete it?
Optimal way for retrieving a random element from a set?
Removing random element from list
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….
Videos
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.
I am working on a program where I need to select a random element from a set instantly. Is this possible?
def select_random(options: Set[int]) -> int:
#Retrieve random element from a set in O(1) time
...I could work with another data structure to bypass the need for this, but I'm wondering if what I have here can be done.