Get Random Index from List in Python (4 Examples)

 

In this post, you’ll learn how to get a random index from a list in the Python language.

Here is an overview of the tutorial:

Let’s dive into it!

 

Load Library & Create Sample List

The first step in this tutorial will be to import the random module.

import random

Then, we will create a sample list as follows.

my_list = [1, 'two', 3.5, 4, 5]
print(my_list)
# [1, 'two', 3.5, 4, 5]

As you can see, my_list is a mixed-type list that contains three integers (e.g., 1), one string (‘two’), and one float (3.5). Now, we are ready to retrieve a random index from this list using several examples.

 

Example 1: Get Random Index Using random.choice() Function

The following Python syntax shows how to use the random.choice() function to randomly select an index from my_list.

random_index = random.choice(range(len(my_list)))
print(random_index)
# 3

Here, the random.choice() function chose a random number from 0 (the lowest valid index) to len(my_list) - 1 (the highest valid index) by using the range() and the len() functions. The selected random number, which in our case is 3, was then assigned to the random_index variable and finally printed into the console.

 

Example 2: Get Random Index Using random.randint() Function

This example is very similar to Example 1. It uses the randint() function of the random module to retrieve a random index in my_list. Take a look.

random_index = random.randint(0, len(my_list) - 1)
print(random_index)
# 4

Here, the randint() function generated a random integer between 0 and len(my_list) - 1. The randomly generated index was then assigned to random_index and printed to the console, which in this case is 4.

 

Example 3: Get Random Index Using random.random() Function

Another function of the random module that can be helpful to select a random index from my_list is the random() function. It works as follows.

random_index = int(random.random() * len(my_list))
print(random_index)
# 3

In the previous Python output, the random() function generated a floating-point number between 0 and 1. Multiplied by the length of my_list, it results in a random float value between 0 and len(my_list). This float value is converted to an integer by using the int() function and finally printed to the console.

 

Example 4: Get Random Index Using NumPy

In this last example, we will use the NumPy library to select a random index from my_list. To do so, we will first load the library.

import numpy as np

Now, we can use the random.randint() function to obtain a random index from our list as follows.

random_index = np.random.randint(0, len(my_list))
print(random_index)
# 0

The code from above shows how np.random.randint(0, len(my_list)) generates a random integer between 0 and len(my_list), which ensures that the generated index is within the valid bounds of the list’s indices.

 

Video, Further Resources & Summary

Do you need more explanations on how to retrieve a random index from a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

Besides that, you could have a look at some of the related tutorials on Statistics Globe:

This post has shown how to get a random index from a list in Python. Please let me know in the comments section below if you have additional questions.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Top