
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Minimum of Each Index in List of Lists in Python
In some problems we need to identify the minimum of each element in a list. But in solving the matrix operations, we need to find the minimum of each column in the matrix. That needs us to find the minimum value from list of lists. Because each column of a matrix is a list of lists.
Using min() and zip()
In the below example we use the min() and zip(). Here the zip() function organizes the elements at the same index from multiple lists into a single list. Then we apply the min() function to the result of zip function using a for loop.
Example
List = [[90, 5, 46], [71, 33, 2], [9, 13, 70]] print("List : " + str(List)) # using min()+ zip() result = [min(index) for index in zip(*List)] print("minimum of each index in List : " + str(result))
Running the above code gives us the following result:
List : [[90, 5, 46], [71, 33, 2], [9, 13, 70]] minimum of each index in List : [9, 5, 2]
Using map() , min() and zip()
We can also use the map() and zip() together in a similar approach as above. Here we have the result of zip() applied to the min(). In place of for loop we use the map() for this purpose.
Example
List = [[0.5, 2.4, 7], [5.5, 1.9, 3.2], [8, 9.9, 10]] print("The list values are: " + str(List)) # using min() + map() + zip() result = list(map(min, zip(*List))) #result print("Minimum of each index in list of lists is : " + str(result))
Running the above code gives us the following result:
The list values are: [[0.5, 2.4, 7], [5.5, 1.9, 3.2], [8, 9.9, 10]] Minimum of each index in list of lists is : [0.5, 1.9, 3.2]