Python | Select random value from a list

05 Jul, 2022
In this article, we will cover how to Randomly Select Elements From a List in Python. 
Generating random numbers has always been a useful utility in day-day programming for games or various types of gambling etc. Hence knowledge and shorthands of it in any programming language is always a plus to have. Let’s discuss all the different ways to select random values from a list. 
In Python random choice() function to select a random item from a list, This random.choice() function is designed for the specific purpose of getting a Random sampling from a list in Python from the container and hence is the most common method to achieve this task of getting a random number from a list. 
Python3
import random # initializing listtest_list = [1, 4, 5, 2, 7] # printing original listprint("Original list is : " + str(test_list)) # using random.choice() to# get a random numberrandom_num = random.choice(test_list) # printing random numberprint("Random selected number is : " + str(random_num))
Output:
Original list is : [1, 4, 5, 2, 7]
Random selected number is : 1
This method is used to generate a random number in a range, for lists, we can specify the range to be 0 to its length, and get the index, and then the corresponding value. This also gives the option of getting even placed elements or elements at the index of a certain multiple. 
Python3
import random # initializing listtest_list = [1, 4, 5, 2, 7] # printing original listprint("Original list is : " + str(test_list)) # using random.randrange() to# get a random numberrand_idx = random.randrange(len(test_list))random_num = test_list[rand_idx] # printing random numberprint("Random selected number is : " + str(random_num))
Output:
Original list is : [1, 4, 5, 2, 7]
Random selected number is : 7
Yet another method to generate the random number, this also can be used to generate any number in a range, and then using that number index, we can find the value at the corresponding index, just like the above-mentioned technique. But it differs by the fact that it required 2 mandatory arguments for range. 
Python3
import random # initializing listtest_list = [1, 4, 5, 2, 7] # printing original listprint("Original list is : " + str(test_list)) # using random.randint() to# get a random numberrand_idx = random.randint(0, len(test_list)-1)random_num = test_list[rand_idx] # printing random numberprint("Random selected number is : " + str(random_num))
Output:
Original list is : [1, 4, 5, 2, 7]
Random selected number is : 4
This method generates the floating-point numbers in the range of 0 to 1. We can also get the index value of list using this function by multiplying the result and then typecasting to int to get the integer index and then the corresponding list value. 
Python3
import random # initializing listtest_list = [1, 4, 5, 2, 7] # printing original listprint("Original list is : " + str(test_list)) # using random.random() to# get a random numberrand_idx = int(random.random() * len(test_list))random_num = test_list[rand_idx] # printing random numberprint("Random selected number is : " + str(random_num))
Output:
Original list is : [1, 4, 5, 2, 7]
Random selected number is : 7
Python has a built-in function called random.sample(). The random module contains the random.sample function (). The ability to choose multiple items from a list is helpful.
Python3
import randomtest_list = [1, 4, 5, 2, 7]print("Original list is : " + str(test_list)) print("Random element is :", random.sample(test_list, 5))
Output:
Original list is : [1, 4, 5, 2, 7]
Random element is : [7, 4, 1, 5, 2]
The random.choices function is stored in the random module (). Selecting numerous items from a list or a single item from a particular sequence is handy.
Python3
import randomtest_list = [11, 44, 55, 22, 77]print("Original list is : " + str(test_list)) print("Random element is :", random.choices(test_list, k=4))
Output:
Original list is : [11, 44, 55, 22, 77]
Random element is : [11, 11, 44, 77]
surajkumarguptaintern
Python list-programs
python-list
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.