Open In App

Python | Maximum absolute difference list of list

Last Updated : 05 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 discuss certain ways in which this task can be performed. 

Method #1: Using max() + 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 the maximum is found using max function, all bounded by list comprehension. 

Python3




# Python3 code to demonstrate
# Maximum absolute difference list of list
# using max() + 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 max() + abs() + zip() + list comprehension
# Maximum absolute difference list of list
res = [max(abs(i - j) for i, j in zip(*ele))
    for ele in zip(test_list, test_list[1:])]
 
# print result
print("The maximum difference sublist : " + str(res))


Output : 

The original list : [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
The maximum difference sublist : [3, 6, 8]

Method #2: Using max() + map() + abs + zip() This task can also be achieved using the combination of the 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
# Maximum absolute difference list of list
# using max() + 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 max() + map() + abs + zip()
# Maximum absolute difference list of list
res = [max(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 maximum difference sublist : " + str(res))


Output : 

The original list : [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
The maximum difference sublist : [3, 6, 8]

Method #3 :itertools.starmap() 

You can use the itertools.starmap() function to compute the maximum difference between the elements at each index. The starmap() function applies a function to each element in an iterable and returns the result. You can use this function in combination with the max() function to compute the maximum difference between the elements at each index.

Here is an example of how you can use the itertools.starmap() function to solve the problem of finding the maximum difference between the elements at each index in a list of lists:

Python3




import itertools
 
# Initialize list of lists
test_list = [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
# Define a function that takes in two lists and returns the maximum difference
# between the elements at each index
def max_difference(list1, list2):
  return max(abs(x - y) for x, y in zip(list1, list2))
 
# Use itertools.starmap() to apply the max_difference() function to each
# consecutive pair of lists in the test_list
result = list(itertools.starmap(max_difference, zip(test_list, test_list[1:])))
 
# Print the result
print(result)
#This code is contributed by Edula Vinay Kumar Reddy


Output

[3, 6, 8]

The overall time complexity of the code that uses the itertools.starmap() function to compute the maximum difference between the elements at each index in a list of lists is O(n), where n is the number of elements in the list of lists. This is because the starmap() function iterates over each element in the iterable and applies the function to it, and the max_difference() function iterates over each element in the two lists and computes the maximum difference between the elements at each index.

The space complexity of this code is also O(n), because the result list will have a size equal to the number of elements in the list of lists.

Method 4: Using a for loop

  • Initialize an empty list res to store the maximum absolute differences between the elements of adjacent lists.
  • Iterate over the indices of the elements of the lists using a nested for loop.
  • For each pair of corresponding elements from two adjacent lists, we calculate their absolute difference using the abs() function.
  • Keep track of the maximum difference found so far in the variable max_diff.
  • After iterating over all the pairs of elements, we append the maximum difference to the list res.
  • Finally, print the result.

Python3




# Python3 code to demonstrate
# Maximum absolute difference list of list
# using for loop
 
# 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 for loop
# Maximum absolute difference list of list
res = []
for i in range(len(test_list)-1):
    max_diff = 0
    for j in range(len(test_list[i])):
        diff = abs(test_list[i][j] - test_list[i+1][j])
        if diff > max_diff:
            max_diff = diff
    res.append(max_diff)
 
# print result
print("The maximum difference sublist : " + str(res))


Output

The original list : [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
The maximum difference sublist : [3, 6, 8]

Time complexity: O(n*m), where n is the number of lists and m is the length of each list.
Auxiliary space: O(n), to store the list res.

Method #5: Using numpy

In this method, we will use the numpy package to compute the maximum absolute difference between the list of lists. We can use the numpy.diff() function to compute the absolute difference between adjacent elements along the rows or columns of the 2D array. Then we can take the maximum value of each row to get the maximum absolute difference for that row.

Step 1: Import numpy package.
Step 2: Convert the list of lists to a numpy array.
Step 3: Compute the absolute difference between adjacent elements along the rows using numpy.diff() function.
Step 4: Take the maximum value of each row using numpy.max() function.
Step 5: Convert the resulting array to a list using numpy.ndarray.tolist() function.

Python3




# Python3 code to demonstrate
# Maximum absolute difference list of list
# using numpy
 
# importing required package
import numpy as np
 
# 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 numpy
# Maximum absolute difference list of list
arr = np.array(test_list)
diff = np.abs(np.diff(arr, axis=0))
res = np.max(diff, axis=1).tolist()
 
# print result
print("The maximum difference sublist : " + str(res))


OUTPUT : 
The original list : [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
The maximum difference sublist : [3, 6, 8]

Time Complexity: O(nm), where n is the number of rows and m is the number of columns in the list of lists. function.
Auxiliary Space: O(nm), where n is the number of rows and m is the number of columns in the list of lists.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads