Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Select random value from a list

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 31 Aug, 2022
Improve Article
Save Article

Given a list and our task is to randomly select elements from the list in Python using various functions. Selecting random numbers from a list can be used sometimes while building games, choosing a random range, etc. 

Example :

Input: [2, 3, 4 , 5, 6 ]
Output: 2
Input: [3, 5, 6, 3, 2]
Output: 6

Using random.choice()  to select random value from a list

This random.choice() function is designed for getting a Random sampling from a list in Python and hence is the most common method to achieve this task of fetching a random number from a list. 

Python3




import random
 
# initializing list
test_list = [1, 4, 5, 2, 7]
 
# printing original list
print("Original list is : " + str(test_list))
 
# using random.choice() to
# get a random number
random_num = random.choice(test_list)
 
# printing random number
print("Random selected number is : " + str(random_num))

Output:

Original list is : [1, 4, 5, 2, 7]
Random selected number is : 1

Using random.randrange() to select random value from a list

random.randrange() method is used to generate a random number in a given range, we can specify the range to be 0 to the length of the list, and get the index, and then the corresponding value.

Python3




import random
 
# initializing list
test_list = [1, 4, 5, 2, 7]
 
# printing original list
print("Original list is : " + str(test_list))
 
# using random.randrange() to
# get a random number
rand_idx = random.randrange(len(test_list))
random_num = test_list[rand_idx]
 
# printing random number
print("Random selected number is : " + str(random_num))

Output:

Original list is : [1, 4, 5, 2, 7]
Random selected number is : 7

Using random.randint() to select random value from a list

random.randint() is used to generate the random number, also this can be used to generate any number in a range, and then using that number, we can find the value at the corresponding index, just like the above-mentioned technique. But it differs by the fact that it requires 2 mandatory arguments for range. 

Python3




import random
 
# initializing list
test_list = [1, 4, 5, 2, 7]
 
# printing original list
print("Original list is : " + str(test_list))
 
# using random.randint() to
# get a random number
rand_idx = random.randint(0, len(test_list)-1)
random_num = test_list[rand_idx]
 
# printing random number
print("Random selected number is : " + str(random_num))

Output:

Original list is : [1, 4, 5, 2, 7]
Random selected number is : 4

Using random.random() to select random value from a list

random.random() 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 it to integer so as to get the integer index and then the corresponding list value. 

Python3




import random
 
# initializing list
test_list = [1, 4, 5, 2, 7]
 
# printing original list
print("Original list is : " + str(test_list))
 
# using random.random() to
# get a random number
rand_idx = int(random.random() * len(test_list))
random_num = test_list[rand_idx]
 
# printing random number
print("Random selected number is : " + str(random_num))

Output:

Original list is : [1, 4, 5, 2, 7]
Random selected number is : 7

Using random.sample() to select random value from a list

Python has a built-in function called random.sample(). The random module contains the random.sample() function. It has the ability to choose multiple items from a list.

Python3




import random
test_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]

Using random.choices() to select random value from a list

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 with the help of random.choices function.

Python3




import random
test_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]

Select k random value from a list

Here we have grouped all elements in a pair of size k.

Python3




import random
 
def select_random_Ns(l, k):
    random.shuffle(l)
    res = []
    for i in range(0, len(l), k):
        res.append(l[i:i + k])
    return res
         
l = ['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
 
print(select_random_Ns(l, 3))

Output:

[['G', 'G', 'R'], ['K', 'K', 'E'], ['O', 'F', 'E'], ['S', 'E', 'S'], ['E']]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!