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:
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
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
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.
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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)
Alternative to the
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)
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.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)
Note that the probabilities we provided sum up to one.
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 –