Python program to select Random value form list of lists

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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 random element from that row.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 = 1
  
# choice() for random number, from_iterables for flattening
res = random.choice(test_list[r_no])
  
# printing result
print("Random number from Matrix Row : " + str(res))

chevron_right


Output

The original list is : [[4, 5, 5], [2, 7, 4], [8, 6, 3]]
Random number from Matrix Row : 7



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.