Assume I have the following list:
foo = [\'a\', \'b\', \'c\', \'d\', \'e\']
What is the simplest way to retrieve an item at random from thi
import random
my_list = [1, 2, 3, 4, 5]
num_selections = 2
new_list = random.sample(my_list, num_selections)
randIndex = random.sample(range(len(my_list)), n_selections)
randIndex.sort()
new_list = [my_list[i] for i in randIndex]
Duplicate of https://stackoverflow.com/a/49682832/4383027
foo = ['a', 'b', 'c', 'd', 'e']
number_of_samples = 1
In python 2:
random_items = random.sample(population=foo, k=number_of_samples)
In python 3:
random_items = random.choices(population=foo, k=number_of_samples)
You could just:
from random import randint
foo = ["a", "b", "c", "d", "e"]
print(foo[randint(0,4)])