Python - Maximum of each Column
Last Updated :
13 Apr, 2023
Sometimes, we are encountered such problems in which we need to find the maximum of each column in a matrix i.e maximum 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.
Method #1: Using for loop+ max()
Python3
# Python3 code to demonstrate
# Maximum of each Column
# initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
# printing original list
print("The original list : " + str(test_list))
# Maximum of each Column
res=[]
for i in range(0,len(test_list)):
x=[]
for j in range(0,len(test_list[i])):
a=test_list[j][i]
x.append(a)
res.append(max(x))
# print result
print("The Maximum of each index list is : " + str(res))
OutputThe original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Maximum of each index list is : [9, 7, 6]
Time Complexity: O(n),The above code iterates through the list once, hence the time complexity is linear, i.e. O(n).
Space Complexity: O(n),The algorithm uses an additional list to store the result, thus consuming linear space which is O(n).
Method #2: Using max() + list comprehension + zip()
The combination of above methods are required to solve this particular problem. The max function is used to get the required maximum value and zip function provides the combination of like indices and then list is created using list comprehension.
Python3
# Python3 code to demonstrate
# Maximum of each Column
# using max() + 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 max() + list comprehension + zip()
# Maximum of each Column
res = [max(idx) for idx in zip(*test_list)]
# print result
print("The Maximum of each index list is : " + str(res))
OutputThe original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Maximum of each index list is : [9, 7, 6]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3: Using map() + max() + zip()
This works in an almost similar way as the above method, but the difference is just that we use map function to build the max element list rather than using list comprehension.
Python3
# Python3 code to demonstrate
# Maximum index value
# using max() + 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 max() + map() + zip()
# Maximum index value
res = list(map(max, zip(*test_list)))
# print result
print("The Maximum of each index list is : " + str(res))
OutputThe original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Maximum of each index list is : [9, 7, 6]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #4: Using numpy()
Note: Install numpy module using command "pip install numpy"
Another approach to find the maximum of each column in a matrix can be using the numpy library.
Python3
#Importing numpy library
import numpy as np
#Initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
#Converting list to 2-D numpy array
arr = np.array(test_list)
#Finding maximum of each column
result = np.amax(arr, axis=0)
#Printing result
print("The Maximum of each index list is:", result)
#This code is contributed by Edula Vinay Kumar Reddy
Output:
The Maximum of each index list is: [9 7 6]
Time Complexity: O(m * n), where m is the number of rows and n is the number of columns in the matrix.
Auxiliary Space: O(n), where n is the number of elements in the result array.
Similar Reads
Get Maximum of Each Key Dictionary List - Python We are given a list of dictionaries and our task is to find the maximum value for each key across all dictionaries. If a key appears in multiple dictionaries we take the highest value among them. For example: a = [{'a': 3, 'b': 8}, {'a': 10, 'b': 2, 'c': 5}, {'c': 12, 'a': 7}] then the output will b
2 min read
Python Program Maximum Of Four Python, a versatile and widely used programming language, offers multiple ways to find the maximum value among four given numbers. Whether you're a beginner or an experienced developer, understanding different approaches can enhance your problem-solving skills. In this article, we will explore simpl
2 min read
Python - Get maximum of Nth column from tuple list Sometimes, while working with Python lists, we can have a task in which we need to work with tuple list and get the maximum of its Nth index. This problem has application in web development domain while working with data information. Letâs discuss certain ways in which this task can be performed. Me
7 min read
Python - Maximum column values in mixed length 2D List The usual list of list, unlike conventional C type Matrix, can allow the nested list of lists with variable lengths, and when we require the maximizations of its columns, the uneven length of rows may lead to some elements in that elements to be absent and if not handled correctly, may throw an exce
6 min read
Python | Positions of maximum element in list Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
3 min read
Python | Index Maximum among Tuples Sometimes, while working with records, we might have a common problem of maximizing contents of one tuple with corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Letâs discuss certain ways in which this task can be performed. Metho
6 min read