Python – Maximum difference across lists
Last Updated :
11 May, 2023
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 : 8
Explanation : 9 – 1 = 8 is maximum difference across lists in same index.
Input : test_list1 = [3, 4, 2, 1, 17], test_list2 = [6, 2, 1, 9, 1]
Output : 16
Explanation : 17 – 1 = 16 is maximum difference across lists in same index.
Method 1 : Using list comprehension + max()
In this, difference is computed using abs() and iteration is done using list comprehension. The max() is used for the task of getting maximum difference among computed sub result.
Python3
test_list1 = [ 3 , 4 , 2 , 1 , 7 ]
test_list2 = [ 6 , 2 , 1 , 9 , 1 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = max ( abs (test_list2[idx] - test_list1[idx])
for idx in range ( len (test_list1)))
print ( "Maximum difference among lists : " + str (res))
|
Output
The original list 1 is : [3, 4, 2, 1, 7]
The original list 2 is : [6, 2, 1, 9, 1]
Maximum difference among lists : 8
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using zip() + max()
In this pairing of like index elements is done using zip(). Rest all the functionality is similar to above method.
Python3
test_list1 = [ 3 , 4 , 2 , 1 , 7 ]
test_list2 = [ 6 , 2 , 1 , 9 , 1 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = max ( abs (ele1 - ele2) for ele1, ele2 in zip (test_list1, test_list2))
print ( "Maximum difference among lists : " + str (res))
|
Output:
The original list 1 is : [3, 4, 2, 1, 7]
The original list 2 is : [6, 2, 1, 9, 1]
Maximum difference among lists : 8
Time Complexity: O(n) where n is the number of elements in the list “test_list”. The zip() + max() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size O(n) is required
Method #3: Using abs(), for loop
Approach:
- First we need to check if both lists have the same length or not.
- If not, then return “Lists have different lengths”.
- Then iterate through both lists at the same time using a for loop and calculate the absolute difference between each corresponding element of both lists.
- Keep track of the maximum difference found so far using a variable.
- Return the maximum difference found.
Python3
def max_diff_lists(list1, list2):
if len (list1) ! = len (list2):
return "Lists have different lengths"
max_diff = 0
for i in range ( len (list1)):
diff = abs (list1[i] - list2[i])
if diff > max_diff:
max_diff = diff
return max_diff
test_list1 = [ 3 , 4 , 2 , 1 , 7 ]
test_list2 = [ 6 , 2 , 1 , 9 , 1 ]
print ( "The original list 1: " , test_list1)
print ( "The original list 2: " , test_list2)
print ( "Maximum difference among lists is: " , max_diff_lists(test_list1, test_list2))
|
Output
The original list 1: [3, 4, 2, 1, 7]
The original list 2: [6, 2, 1, 9, 1]
Maximum difference among lists is: 8
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...