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
Python - Find maximum length sub-list in a nested list
In Python, we often work with nested lists (lists inside lists), and sometimes we need to find out which sub-list has the most items. In this article, we will explore Various methods to Find the maximum length of a sub-list in a nested list. Using max() Function with key=lenThe simplest and most eff
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
Flatten a List of Lists in Python
Flattening a list of lists means turning a nested list structure into a single flat list. This can be useful when we need to process or analyze the data in a simpler format. In this article, we will explore various approaches to Flatten a list of Lists in Python.Using itertools.chain itertools modul
3 min read
Python | Find mismatch item on same index in two list
Given two list of integers, the task is to find the index at which the element of two list doesn't match. Input: Input1 = [1, 2, 3, 4] Input2 = [1, 5, 3, 6] Output: [1, 3] Explanation: At index=1 we have 2 and 5 and at index=3 we have 4 and 6 which mismatches. Below are some ways to achieve this tas
4 min read
Find Median of List in Python
Sometimes, while working with Python list we can have a problem in which we need to find Median of list. This problem is quite common in the mathematical domains and generic calculations. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + "~" operator This task
4 min read
Python | Indices of N largest elements in list
Sometimes, while working with Python lists, we can have a problem in which we wish to find N largest elements. This task can occur in many domains such as web development and while working with Databases. We might sometimes, require to just find the indices of them. Let's discuss a certain way to fi
5 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
Python | Check if a list exists in given list of lists
Given a list of lists, the task is to check if a list exists in given list of lists. Input : lst = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]] list_search = [4, 5, 6] Output: True Input : lst = [[5, 6, 7], [12, 54, 9], [1, 2, 3]] list_search = [4, 12, 54] Output: False Letâs discuss certain ways
4 min read