Python program to select Random value from list of lists
Last Updated :
08 May, 2023
Given a list of lists. The task is to extract a random element from it.
Examples:
Input : test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]]
Output : 7
Explanation : Random number extracted from Matrix.
Input : test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]], r_no = 2
Output : 6
Explanation : Random number extracted from 2nd row from Matrix.
Method #1: Using chain.from_iterable() + random.choice()
In this, we flatten the Matrix to list using from_iterable() and choice() is used to get a random number from the list.
Python3
from itertools import chain
import random
test_list = [[ 4 , 5 , 5 ], [ 2 , 7 , 4 ], [ 8 , 6 , 3 ]]
print ( "The original list is : " + str (test_list))
res = random.choice( list (chain.from_iterable(test_list)))
print ( "Random number from Matrix : " + str (res))
|
Output
The original list is : [[4, 5, 5], [2, 7, 4], [8, 6, 3]]
Random number from Matrix : 6
Method #2 : Using choice() to get element from particular row
If a row is mentioned, the choice() method can be used to get a random element from that row.
Python3
import random
test_list = [[ 4 , 5 , 5 ], [ 2 , 7 , 4 ], [ 8 , 6 , 3 ]]
print ( "The original list is : " + str (test_list))
r_no = [ 0 , 1 , 2 ]
res = random.choice(test_list[random.choice(r_no)])
print ( "Random number from Matrix Row : " + str (res))
|
Output
The original list is : [[4, 5, 5], [2, 7, 4], [8, 6, 3]]
Random number from Matrix Row : 7
Method 3: Using the numpy package.
Approach:
- Import the numpy package.
- Initialize the 2D matrix using the numpy array() method.
- Use the numpy random.choice() method to generate a random number from the matrix.
- Print the result.
Python3
import numpy as np
import random
test_list = [[ 4 , 5 , 5 ], [ 2 , 7 , 4 ], [ 8 , 6 , 3 ]]
print ( "The original list is : " + str (test_list))
r_no = [ 0 , 1 , 2 ]
arr = np.array(test_list)
res = np.random.choice(arr[random.choice(r_no)])
print ( "Random number from Matrix Row : " + str (res))
|
Output
The original list is : [[4, 5, 5], [2, 7, 4], [8, 6, 3]]
Random number from Matrix Row : 7
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 3 : Using list comprehension and random.sample()
Approach:
- Import the random module
- Initialize the list and print the original list
- Generate a random number for the row index using random.randint() method
- Use list comprehension to extract the row corresponding to the random index generated in step 3
- Use the random.sample() method to get a sample of elements from the row, with a sample size of 1.
- Get the randomly selected element from the sample and store it in a variable.
- Print the randomly selected element from the matrix row.
Python3
import random
test_list = [[ 4 , 5 , 5 ], [ 2 , 7 , 4 ], [ 8 , 6 , 3 ]]
print ( "The original list is : " + str (test_list))
row_index = random.randint( 0 , len (test_list) - 1 )
row = [test_list[row_index][i] for i in range ( len (test_list[row_index]))]
sample = random.sample(row, k = 1 )
res = sample[ 0 ]
print ( "Random number from Matrix Row : " + str (res))
|
Output
The original list is : [[4, 5, 5], [2, 7, 4], [8, 6, 3]]
Random number from Matrix Row : 7
Time Complexity: O(n) – linear time complexity as we are traversing the matrix once using list comprehension to extract the row.
Auxiliary Space: O(n) – linear auxiliary space as we are creating a new list to store the extracted row.
Please Login to comment...