Python | Minimum Difference in Matrix Columns
Last Updated :
17 Apr, 2023
This particular article focuses on a problem that has utility in competitive as well as day-day programming. Sometimes, we need to get the minimum difference between the like indices when compared with the next list. The minimum difference between the like elements in that index is returned. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using min() + abs() + zip() + list comprehension This particular problem can also be solved using the combination of the above 4 operations. Here zip function does the dual task of pairing the list and also pairing the like indices for difference, to be computed by abs function and then minimum is found using min function, all bounded by list comprehension.
Python3
# Python3 code to demonstrate
# Minimum Difference in Matrix Columns
# using min() + abs() + zip() + list comprehension
# initializing list
test_list = [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
# printing original list
print("The original list : " + str(test_list))
# using min() + abs() + zip() + list comprehension
# Minimum Difference in Matrix Columns
res = [min(abs(i - j) for i, j in zip(*ele)) for ele in zip(test_list, test_list[1:])]
# print result
print("The minimum difference sublist : " + str(res))
Output : The original list : [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
The minimum difference sublist : [1, 3, 2]
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 #2 : Using min() + map() + abs + zip() This task can also be achieved using the combination of above functions, the addition is map function that performs the task of binding of abs operation to the whole list.
Python3
# Python3 code to demonstrate
# Minimum Difference in Matrix Columns
# using min() + map() + abs + zip()
# initializing list
test_list = [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
# printing original list
print("The original list : " + str(test_list))
# using min() + map() + abs + zip()
# Minimum absolute difference list of list
res = [min(map(abs, (i - j for i, j in zip(x, y)))) for x, y in zip(test_list, test_list[1:])]
# print result
print("The minimum difference sublist : " + str(res))
Output : The original list : [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
The minimum difference sublist : [1, 3, 2]
Similar Reads
Python - Maximum difference across lists Given two lists, the task is to write a Python program to find maximum difference among like index elements. Examples: Input : test_list1 = [3, 4, 2, 1, 7], test_list2 = [6, 2, 1, 9, 1]Output : 8Explanation : 9 - 1 = 8 is maximum difference across lists in same index. Input : test_list1 = [3, 4, 2,
4 min read
Python | Find Maximum difference pair Sometimes, we need to find the specific problem of getting the pair which yields the maximum difference, this can be solved by sorting and getting the first and last elements of the list. But in some case, we don't with to change the ordering of list and perform some operation in a similar list with
5 min read
Python | Row with Minimum element in Matrix We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but sometimes we need to print the entire row containing it and having shorthands to perform the same are always helpful as this kind of problem can come
5 min read
Python | Maximum absolute difference list of list This particular article focuses on a problem that has utility in competitive as well as day-day programming. Sometimes, we need to get the maximum difference between the like indices when compared with the next list. The maximum difference between the like elements in that index is returned. Let's d
7 min read
Python - Uneven Sized Matrix Column Minimum 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 minimum 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 exception. Le
7 min read
Python - Consecutive Kth column Difference in Tuple List Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the absolute difference of itâs Kth index. This problem has application in web development domain while working with data informations. Letâs discuss certain ways in which this task can be
6 min read