Given a list of lists. The task is to extract a random element from it.
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()
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.
The original list is : [[4, 5, 5], [2, 7, 4], [8, 6, 3]] Random number from Matrix Row : 7
- Python | Select random value from a list
- Python | Program to count number of lists in a list of lists
- Python - Convert Lists into Similar key value lists
- random.random() function in Python
- Python - Convert Key-Value list Dictionary to List of Lists
- Random sampling in numpy | random() function
- Python | Zipping two lists of lists
- Python | Sort list of lists by lexicographic value and then length
- Python | Split list into lists by particular value
- Python | Convert list of tuples to dictionary value lists
- How to select a drop-down menu value using Selenium in Python?
- Python | Maximum sum of elements of list in a list of lists
- Python | Ways to sum list of lists and return sum list
- Python | Check if a list exists in given list of lists
- Python | Convert list into list of lists
- Python | Sorting list of lists with similar list elements
- Python | Merge List with common elements in a List of Lists
- Python | Convert List of lists to list of Strings
- Python - Convert List of Dictionaries to List of Lists
- Randomly select n elements from list in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.