Open In App
Related Articles

Python program to select Random value from list of lists

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

# Python3 code to demonstrate working of
# Random Matrix Element
# Using chain.from_iterables() + random.choice()
from itertools import chain
import random
 
# Initializing list
test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# choice() for random number, from_iterables for flattening
res = random.choice(list(chain.from_iterable(test_list)))
 
# Printing result
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

# Python3 code to demonstrate working of
# Random Matrix Element
# Using random.choice() [if row number given]
import random
 
# initializing list
test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Row number
r_no = [0, 1, 2]
 
# choice() for random number, from_iterables for flattening
res = random.choice(test_list[random.choice(r_no)])
 
# printing result
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:

  1. Import the numpy package.
  2. Initialize the 2D matrix using the numpy array() method.
  3. Use the numpy random.choice() method to generate a random number from the matrix.
  4. Print the result.

Python3

import numpy as np
import random
 
# initializing list
test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Row number
r_no = [0, 1, 2]
 
# Converting list to numpy array
arr = np.array(test_list)
 
# Generating random number from matrix
# using numpy random.choice() method
res = np.random.choice(arr[random.choice(r_no)])
 
# Printing result
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:

  1. Import the random module
  2. Initialize the list and print the original list
  3. Generate a random number for the row index using random.randint() method
  4. Use list comprehension to extract the row corresponding to the random index generated in step 3
  5. Use the random.sample() method to get a sample of elements from the row, with a sample size of 1.
  6. Get the randomly selected element from the sample and store it in a variable.
  7. Print the randomly selected element from the matrix row.

Python3

import random
 
# initializing list
test_list = [[4, 5, 5], [2, 7, 4], [8, 6, 3]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# generating random row index
row_index = random.randint(0, len(test_list)-1)
 
# extracting the row corresponding to the random index using list comprehension
row = [test_list[row_index][i] for i in range(len(test_list[row_index]))]
 
# getting a sample of elements from the row using random.sample()
sample = random.sample(row, k=1)
 
# getting the randomly selected element from the sample
res = sample[0]
 
# printing result
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.



Last Updated : 08 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads