Python | Find minimum of each index in list of lists

Sometimes, we are encountered with such problem in which we need to find the minimum of each column in a matrix i.e minimum of each index in list of lists. This kind of problem is quite common and useful in competitive programming. Let’s discuss certain ways in which this problem can be solved.

Examples:

Input : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
Output : [1, 3, 2] 
 
Input : [[3, 2, 8], [5, 3, 5], [9, 3, 1]] 
Output : [3, 2, 1]

Method #1 : Using min() + list comprehension + zip()



The combination of above methods are required to solve this particular problem. The min function is used to get the required minimum value and zip function provides the combination of like indices and then list is created using list comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Minimum index value
# using min() + list comprehension + zip()
  
# initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
  
# printing original list
print("The original list : " + str(test_list))
  
# using min() + list comprehension + zip()
# Minimum index value
res = [min(idx) for idx in zip(*test_list)]
  
# print result
print("The Minimum of each index list is : " + str(res))

chevron_right


Output :

The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is : [1, 3, 2]

 

Method #2 : Using map() + min() + zip()

This works in almost similar way as the above method, but the difference is just that we use map function to build the min element list rather than using list comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Minimum index value
# using min() + map() + zip()
  
# initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
  
# printing original list
print("The original list : " + str(test_list))
  
# using min() + map() + zip()
# Minimum index value
res = list(map(min, zip(*test_list)))
  
# print result
print("The Minimum of each index list is : " + str(res))

chevron_right


Output :

The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is : [1, 3, 2]


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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.