Python developers may sometimes need to randomly select one or more items from a Python list. It can be difficult to device a solution from scratch, for this purpose. Luckily there are several easy ways to pick a random item from a Python list. In this article, we will learn how to randomly select item from Python list.
Why Randomly Select Item from Python List
If you need to display a random image or blog post on your website, then you may need to select a random item from a list of image or posts. This is a use case where you need to randomly select item from Python list. Another use case is for statistical analysis where you need to select a random sample from a larger data set. In this case also, you need to randomly select item from Python list.
How to Randomly Select Item from Python List
There are several powerful Python libraries that offer many useful functions to generate a random number. Among them, random library is one of the most popular libraries. It offers several functions to help you select one or more random items from a Python list, in different ways.
1. Using random.choice()
Random library provides choice() function that returns a random item from a list of items in Python. Let us say you have the following Python list.
import random
a = [1, 2, 3, 4, 5]
print(random.choice(a)) # output is a random item
In the above code, we first import random library. Then we call random.choice() function with list name as the first argument. Every time you can random.choice() function, it will return a different output.
If you want to select multiple items from a list of items, then you need to use random.choices() instead. It takes two arguments – the list and number of items to be selected. While specifying the number of items, you need to specify the keyword ‘k’.
import random
a = [1, 2, 3, 4, 5]
print(random.choices(a, k=2)) # output is a random list of 2 items
Every time you run the above code, it will print a list of 2 random items from original list. Please note, this list may contain repetitions. If you do not want any repetition in your list of randomly selected items, then you need to use random.sample() function described below.
Also, when you select multiple items using this method, then the result items can be any order. Even the repeated items can be located in any order. Sometimes, only one item may be repeated, sometimes multiple items may be repeated.
2. Using random.sample()
Random.sample() function can be used to select one or more items from a list of items. When you select multiple items, it will return a list of randomly selected items, without any repetition. It has the following syntax.
random.sample(list_name, no_of_items)
Here is an example to select a single item from a list of items.
import random
a = [1, 2, 3, 4, 5]
print(random.sample(a,1)) # output is a random list of 1 item such as [2]
Here is an example to randomly select multiple items from a Python list.
import random
a = [1, 2, 3, 4, 5]
print(random.sample(a,3)) # [1, 4, 2]
Please note, whether you select one random item, or multiple random items, it will always return the result as a list.
Also, when you select multiple items, then the result may contain items in any order. They need not be in the same order as they are present in original list.
3. Using random.random()
Random.random() function returns a random floating point number between 0 and 1. It can be used to get a random item from Python list by multiplying it with the length of Python list and typecasting the result to an integer to obtain random index. This index can be used to pick random item from Python list.
import random
a = [1, 2, 3, 4, 5]
random_number = random.random()
random_index = int(random_number * len(a))
random_item = a[random_index]
print(random_item)
In the above code, first we calculate a random number using random.random() function. We then multiply it with length of our list. We convert the result into an integer using int(), to obtain random index. Lastly, we obtain the random item using this random index.
In fact, this used to be one of the most commonly used methods to generate random number or pick rando item from a Python list, before the advent of out of the box functions.
4. Using random.randrange()
Random.randrange() function allows you to generate a random number within a range. It takes one argument and returns a random number between 0 and that number. In this case, we directly use this function to get a random index value, and use this index to get random item.
import random
a = [1, 2, 3, 4, 5]
random_index = random.randrange(len(a))
random_item = a[random_index]
print(random_item)
In the above code, we can randrange() function on length of our list. It returns an index >= 0 and length of our list-1. We use it to select the random item.
This method allows you to directly get a random index to obtain random item. If you want to obtain a list of multiple random items from a given list then you need to run a loop whereby in each iteration, you call this randrange() function and get a random item. Here is an example.
import random
a = [1, 2, 3, 4, 5]
b = []
n=3
for i in range(n):
random_index = random.randrange(len(a))
random_item = a[random_index]
b.append(random_item)
print(b)
In the above code, we need to get 3 random items from our list. So we create a blank list b to store the result. We run a loop with 3 iterations. In each iteration, we get a random item from our list and push it to our result list. Please note, the order of items in the result list need not be same as that of original list. This is because in each iteration, you may get a different index. Sometimes, there may also be repetition, if the randrange() returns the same index.
5. Using random.randint()
Random.randint() generates a random number between 2 specific numbers. We use this function to get a random number between 0 and length of our list. This will generate a random index. We use this index to get random element.
import random
a = [1, 2, 3, 4, 5]
random_index = random.randint(0,len(a)-1)
random_item = a[random_index]
print(random_item)
If you want to pick multiple random items from a list, you can run a loop for the number of random items. In each loop iteration, you can generate a random index and use it to get a random item. One by one, you can append each of these random items to a result list.
import random
a = [1, 2, 3, 4, 5]
b = []
n=3
for i in range(n):
random_index = random.randint(0,len(a)-1)
random_item = a[random_index]
b.append(random_item)
print(b)
Conclusion
In this article, we have learnt several different ways to randomly select item from Python list. We have learnt how to select single as well as multiple items from a list. You can use any of these methods as per your requirement. If you need only one random item from a list, you can use random.choice() function. If you want to pick multiple random items, then you can use random.choices(), if you are ok with repetitions in your selection. If you do not want any repetition in randomly selected items, then you can use random.sample() function.
Also read:
How to Delete Column in Pandas Dataframe
How to Sort Python Dictionary By Value
How to Rename Columns in Pandas

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.