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 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 maximum is found using max function, all bounded by list comprehension.
# 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)) |
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 above functions, the addition is map function that performs the task of binding of abs operation to the whole list.
# 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)) |
The original list : [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]] The maximum difference sublist : [3, 6, 8]
Recommended Posts:
- Python program to find sum of absolute difference between all pairs in a list
- Python | Find maximum length sub-list in a nested list
- Python | Maximum sum of elements of list in a list of lists
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Set Difference in list of dictionaries
- Python | Triple list difference
- Python | Calculate difference between adjacent elements in given list
- Python | Generate successive element difference list
- Python | Get first element with maximum value in list of tuples
- Python | Maximum and minimum element's position in a list
- Python | Find the sublist with maximum value in given nested list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert list of string into sorted list of integer
- Python | Sorting list of lists with similar list elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



