The list represents a collection of ordered elements in which  each element has a position relative to the other elements in the list.

In this article we will learn how we can pick a random element from a list. There are various ways that this can be achieved.

Using random.choice() and random.choices() functions

The random module in the standard library offers a number of tools for generating and working with  random values. The choice() function in the module selects a single item randomly from a non-empty collection of values.

We  can use the choice() function with a list as the argument to  select a random item from the list.

random.choice(my_list)
#import the choice function from the random module
from random import choice

#the list to select from
my_list = ['Python', 'Java', 'C++', 'Ruby']

#get the random elemet
random_element = choice(my_list)

print(random_element)

In the above example, we imported the choice() function from the random module, then called it with list my_list as the argument to get a random element from the list.

While the choice() function simply picks a single random element, the choices() function in the same module allows us to specify an integer argument which represents the number of random elements that  we want to select.

random.choices(my_list, k = 1)
my_list Required. The list from which to select random elements.
k Optional . An integer representing the number of random elements to be selected. It defaults to 1

If the k parameter is not given, the function only chooses one random element. 

The function returns a new list containing the randomly selected elements. 

from random import choices

my_list = ['Python', 'Java', 'C++', 'PHP', 'Ruby']

random_elements = choices(my_list)
print(random_elements)

#get a single element from the list
print(random_elements[0])

 In the above example:

  • We called the choices() function without specifying the value of k, thus only a single element is selected from the list.
  • The function returns a list containing the random element.
  • We used indexing to get the random element from the returned list.

Using secrets.choice() function

The secrets module in the standard library provides  functionality for generating cryptographically secure random numbers. You should however note that the secrets module is relatively new in Python and will not work with some older versions particularly  Python 3.6 and earlier. 

We can use the choice() function from the module with a list as the argument in order  to get a random element from the list.

secrets.choice(my_list)

The function returns the selected random element, the given list should not be empty, otherwise an error will be raised.

#import the choice function from secrets
from secrets import choice

my_list = ['Japan', 'Russia', 'Singapore', 'Rwanda', 'Canada']

#use the choice function to get a random value
random_element = choice(my_list)

print(random_element)

In the above example:

  • We imported the choice() function from the secrets module
  • Initiated a list with the elements
  • Then we used the choice() function to pick a random element from the list.

Using a random index

The random module provide functions for generating random numbers in a given range, these functions includes; randint(), randrange(), and random() functions. We can use any of these functions to get an integer value in the range of the valid list indices i.e from 0 to the length of the list. We can the use list indexing with the random index to get a random value from the list.

using randint() function

#import the randint() function
from random import randint

my_list = ['Kigali', 'Ottawa', 'Tokyo', 'Helsinki', 'Manilla']

#get a random index
random_index = randint(0, len(my_list))

#Use the random index to get the corresponding list element 
random_element = my_list[random_index]

print(random_element)

In the above example:

  • We imported the randint() function from the random module.
  • The randint() function returns a random integer from the specified range, we specified 0 and the length of the list in order to get an integer representing a valid list index.
  • We used list indexing with the random integer to get the corresponding element from the list.

Using randrange() function

#import the randrange() function
from random import randrange

my_list = ['Kigali', 'Ottawa', 'Tokyo', 'Helsinki', 'Manilla']

random_index = int(randrange(0, len(my_list)))

random_element = my_list[random_index]

print(random_element)

The randrange() function is like the randint() function in that it returns a random number from a given range. However, the randrange() function returns a random whole numbers from that range rather than an integer value. We used the int() function to cast the returned floating point value to a valid integer index, then used the index to get the corresponding list element.

Using random() function

#import the random() function
from random import random

my_list = ['Kigali', 'Ottawa', 'Tokyo', 'Helsinki', 'Manilla']

random_index = int(random() * len(my_list))

random_element = my_list[random_index]

print(random_element)

The random() function does not take any argument, it simply returns a random whole number between 0 and 1. We multiplied the returned whole number with the length of the list then casted it to an integer in order to get a valid list index. We used the resulting integer value to get the corresponding list element.