How to randomly select an item from a list?

How to randomly select an item from a list?

This post was originally posted on Python Engineer's Blog

In this tutorial, we will look at different ways to select a random item from a list. Let's assume you have a list with multiple Twitter user names and you are trying to select a random Twitter user. Below is a sample list of Twitter user names

twitter_user_names = [
 '@rahulbanerjee99',
 '@python_engineer',
 '@FCBarcelona',
 '@FerranTorres20',
 '@elonmusk',
 '@binance',
 '@SpaceX'
]

Random Library

The random library is a built-in python library, i.e you do not have to install it. You can directly import it. We will look at 3 different ways to select a random item from a list using the random library

1. Random Index

import random 
num_items = len(twitter_user_names)
random_index = random.randrange(num_items)
winner = twitter_user_names[random_index]
print(winner)
'''
OUTPUT: @binance
'''

random.randrange(num_items) returns a random number between 0 and num_items - 1. So we basically get a random index that we can use to access an element from our list.

2. Single Random Element

winner = random.choice(twitter_user_names)
print(winner)
'''
OUTPUT: @SpaceX
'''

random.choice takes a sequence like a list as a parameter and returns a random element from the list. In our case, we simply pass the twitter_user_names list.

3. Multiple Random Element

winners = random.sample(twitter_user_names,2)
print(winners)
'''
OUTPUT: ['@python_engineer', '@rahulbanerjee99']
'''

*random.sample is similar to random.choice, the main difference being that you can specify the number of random elements you want. In the above code snippet, I got two random Twitter user names. random.sample returns a list.

In some cases, you might want the same random element(s) to be returned by the random library. The following line of code ensures that the same random element(s) will be generated whenever you run your script. This can be useful when you are debugging and want your script to produce consistent outputs.

random.seed(0)

random.seed takes an integer parameter. If you pass a different parameter other than 0, you will get a different random element(s).

Secrets library

The Secrets library is preferred over the Random library since it is more secure. Like the random library, it is a built-in python library and you do not have to install any dependencies. However if you are using a version below Python 3.6, you will have to install a backport of the secrets module. You can read more about it here

1. Random Index

import secrets
random_index = secrets.randbelow(num_items)
winner = twitter_user_names[random_index]
print(winner)
'''
OUTPUT: @binance
'''

This is similar to random.randrange. We get a random index between 0 and num_items - 1 and use it to access an element from our Twitter user names list.

2. Single Random Element

winner = secrets.choice(twitter_user_names)
print(winner)
'''
OUTOUT: @binance
'''

This is similar to random.choice and returns a random element from the list passed as a parameter.

3. Multiple Random Elements

winners = secrets.SystemRandom().sample(twitter_user_names,2)
print(winners)
'''
OUTPUT ['@SpaceX', '@binance']
'''

This is similar to random.sample and lets you pass the number of rand items you want as a parameter. This method returns a list.

Unlike random.seed, you can not use a seed to keep the random element(s) generated by the secrets library consistent.


Follow me on twitter for more content related to Python, TypeScript and Software Engineering