Python | Find minimum of each index in list of lists
Last Updated :
24 Feb, 2023
Sometimes, we have encountered such problems 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.
Python3
# 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))
Output : The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is : [1, 3, 2]
Time Complexity: O(n*m) where n is the number of sublists in the test_list and m is the length of the sublists.
Auxiliary Space: O(n) where n is the number of sublists in the test_list.
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.
Python3
# 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))
Output : The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is : [1, 3, 2]
Time complexity: O(mn), where m is the number of sublists in the original list and n is the length of each sublist. This is because the zip function takes O(min(m, n)) time to transpose the sublists, the map function takes O(mn) time to apply the min function to each index in the sublists, and the list function takes O(m*n) time to convert the result to a list.
Auxiliary space: O(mn), as it requires a list of size mn to store the result.
Method #3 : Using enumerate()
The function get_min_at_each_index takes in a list of lists lst. It initializes an empty list min_at_each_index to store the minimum value at each index.
It then iterates through each index i in the range 0 to the length of the first list in lst (since all the lists in lst are assumed to have the same length). For each index i, it appends to min_at_each_index the minimum value of the i-th element from each list in lst. It does this by using a list comprehension to create a list of the i-th elements from each list in lst, and then passing this list to the min function.
Python3
# Python3 code to demonstrate
# Minimum index value
def get_min_at_each_index(lst):
min_at_each_index = []
for i in range(len(lst[0])):
min_at_each_index.append(min(row[i] for row in lst))
return min_at_each_index
# initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
# printing original list
print("The original list : " + str(test_list))
# print result
print("The Minimum of each index list is : " , get_min_at_each_index(test_list))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is : [1, 3, 2]
Time complexity: O(n), where n is number of elements in all lists
Auxiliary Space: O(n), to store result
Method #4 : Using min()+for loops
Python3
# Python3 code to demonstrate
# Minimum index value
# initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
# printing original list
print("The original list : " + str(test_list))
# Minimum index value
res=[]
for i in range(0,len(test_list)):
x=[]
for j in range(0,len(test_list[i])):
x.append(test_list[j][i])
res.append(min(x))
# print result
print("The Minimum of each index list is : " + str(res))
OutputThe original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is : [1, 3, 2]
Time complexity: O(n*n), where n is a number of elements in all lists
Auxiliary Space: O(n), to store result
Using NumPy() library
Python3
import numpy as np
# initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
# printing original list
print("The original list : " + str(test_list))
# using numpy
res = np.amin(test_list, axis=0)
# print result
print("The Minimum of each index list is : " + str(res))
#This code is contributed Vinay Pinjala.
Output:
The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Minimum of each index list is : [1, 3, 2]
Time complexity: O(n*n), where n is the number of elements in all lists
Auxiliary Space: O(n), to store result
Method : Using map() and lambda function:
1.Define a list of lists called "test_list".
2.Print the original list using the "print()" function.
3.Use the map function with a lambda function to find the minimum value at each index:
a. Define a lambda function that takes in multiple arguments using the "*" operator.
b. Use the built-in "min()" function to find the minimum value of the input arguments.
c. Apply the lambda function to each set of values in the sublists using the map() function.
d. Convert the resulting map object to a list using the "list()" function.
4.Store the list of minimum values in a new variable called "min_index_values".
5.Print the minimum of each index list using the "print()" function.
Python3
# Define the test list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
# Print the original list
print("The original list:", test_list)
# Use the map function with a lambda function to find the minimum value at each index
min_index_values = list(map(lambda *args: min(args), *test_list))
# Print the minimum of each index list
print("The minimum of each index list is:", min_index_values)
#This code is contributed by Jyothi pinjala.
OutputThe original list: [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The minimum of each index list is: [1, 3, 2]
Time Complexity:
The map() function with a lambda function is used to find the minimum value at each index in the list of lists. Since the map() function has to iterate over all the elements of the input list, its time complexity is O(N), where N is the total number of elements in the list of lists. The use of the built-in "min()" function has a time complexity of O(k), where k is the number of arguments passed to the function. In this case, k is the number of sublists in the input list. Therefore, the time complexity of the code is O(N*k).
Space Complexity:
The space complexity of the code is O(k), where k is the number of sublists in the input list. This is because the map() function returns a map object, which is converted to a list using the "list()" function. The resulting list contains the minimum values at each index of the sublists. Since the size of this list is proportional to the number of sublists in the input list, the space complexity of the code is O(k).
Similar Reads
How to Find Index of Item in Python List To find the index of given list item in Python, we have multiple methods depending on specific use case. Whether weâre checking for membership, updating an item or extracting information, knowing how to get an index is fundamental. Using index() method is the simplest method to find index of list it
2 min read
Position of maximum and minimum element in a list - Python In Python, lists are one of the most common data structures we use to store multiple items. Sometimes we need to find the position or index of the maximum and minimum values in the list. For example, consider the list li = [3, 5, 7, 2, 8, 1].The maximum element is 8, and its index is 4.The minimum e
3 min read
Python - Find Minimum Pair Sum in list Sometimes, we need to find the specific problem of getting the pair which yields the minimum sum, this can be computed by getting initial two elements after sorting. But in some case, we donât with to change the ordering of list and perform some operation in the similar list without using extra spac
4 min read
Python | Minimum K records of Nth index in tuple list Sometimes, while working with data, we can have a problem in which we need to get the minimum of elements filtered by the Nth element of record. This has a very important utility in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using filter() + l
9 min read
Sorting List of Lists with First Element of Each Sub-List in Python In Python, sorting a list of lists by the first element of each sub-list is a common task. Whether you're dealing with data points, coordinates, or any other structured information, arranging the lists based on the values of their first elements can be crucial. In this article, we will sort a list o
3 min read
Python - Find starting index of all Nested Lists In this article given a matrix, the task is to write a Python program to compute the starting index of all the nested lists. Example: Input : test_list = [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]] Output : [0, 1, 5, 7, 13] Explanation : 1 + 4 = lengths of 2 initial lists = 5, 3, of 3
8 min read
Python | Find closest number to k in given list Given a list of numbers and a variable K, where K is also a number, write a Python program to find the number in a list which is closest to the given number K. Examples: Input : lst = [3.64, 5.2, 9.42, 9.35, 8.5, 8], K = 9.1 Output : 9.35 Input : lst = [9, 11, 5, 3, 25, 18], K = 6 Output : 5 Method
5 min read
Python - Minimum in each record value list Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the min of list as tuple attribute. Letâs discuss certain wa
6 min read
Python - Ways to find indices of value in list In Python, it is common to locate the index of a particular value in a list. The built-in index() method can find the first occurrence of a value. However, there are scenarios where multiple occurrences of the value exist and we need to retrieve all the indices. Python offers various methods to achi
3 min read
Python - Alternate Minimum element in list Some of the list operations are quite general and having shorthands without needing to formulate a multiline code is always required. Wanting to construct the list consisting of all the alternate elements of the original list is a problem that one developer faces in day-day applications and sometime
4 min read