In Python, there are many situations where you may need to select a random value from a list. Whether you are building a game, conducting a statistical analysis, or simply shuffling a deck of cards, the ability to choose a random value is a crucial skill. Fortunately, Python provides a built-in module called “random” that makes it easy to generate random values. In this tutorial, we will explore how to use the “random” module to select a random value from a list in Python.

We will cover different methods for generating random values, as well as some common use cases for random selection. By the end of this tutorial, you will have a solid understanding of how to choose a random value from a list in Python.

## How to select a random value from a Python List?

You can use the `choice()`

function available in the `random`

module in Python to get a random value from a list. The syntax for `random.choice()`

is as follows:

random.choice(sequence)

Here, `sequence`

is the iterable object from which a random element is to be selected. The sequence can be a list, tuple, string, or any other iterable object.

## Steps to choose a random value from a list

Let’s now look at a step by step example of using the `random.choice()`

function to get a random value from a list.

### Step 1: Import the random module

The first step is to import the `random`

module. This can be done using the `import`

statement:

import random

### Step 2: Create a list of values

Next, we need to create a list of values from which we want to choose a random value. For example, let’s create a list of fruits:

fruits = ['apple', 'banana', 'orange', 'kiwi', 'mango']

### Step 3: Use the random.choice() function

The `random`

module provides a function called `choice()`

that can be used to choose a random value from a list. This function takes a list as an argument and returns a randomly selected value from that list.

random_fruit = random.choice(fruits) print(random_fruit)

In the above example, we are using the `choice()`

function to choose a random fruit from the `fruits`

list. The randomly selected fruit is then printed to the console.

### Step 4: Run the code

Now that we have written the code, we can run it to see the output. When we run the code, a random fruit from the `fruits`

list will be printed.

import random fruits = ['apple', 'banana', 'orange', 'kiwi', 'mango'] random_fruit = random.choice(fruits) print(random_fruit)

Output:

kiwi

## Alternative to the `random.choice()`

method

There are other methods as well to get a random value from a list in Python. One such method is to use the `random.randint()`

function to generate a random index and then access the element at that index in the list. Here’s an example:

import random fruits = ['apple', 'banana', 'orange', 'kiwi', 'mango'] # Using random.randint() random_index = random.randint(0, len(my_list)-1) random_fruit = fruits[random_index] print(random_fruit)

Output:

orange

Both methods will give you a random element from the list but `random.choice()`

is generally considered to be more concise and readable.

## Choose a random value from a list using a probability distribution

What if you want to select a value from a list and also specify the probability distribution for the list values when sampling?

You can do so using the `random.choice()`

function available in the `numpy`

module.

The `numpy.random.choice()`

function takes a parameter `p`

that you can use to specify the probabilities to use when randomly sampling from the list. Let’s look at an example.

import numpy as np fruits = ['apple', 'banana', 'orange', 'kiwi', 'mango'] probabilities = [0.1, 0.2, 0.3, 0.2, 0.2] random_fruit = np.random.choice(fruits, p=probabilities) print(random_fruit)

Output:

orange

Note that the probabilities we provided sum up to one.

## Conclusion

In this tutorial, we have learned how to choose a random value from a list in Python using the `random`

module. By following the steps outlined in this tutorial, you should now be able to generate random values from a list in your own Python programs.

You might also be interested in –