Given a list, the task is to create a new list containing difference of adjacent elements in the given list.
Method #1: Using zip()
Python3
ini_list = [ 5 , 4 , 89 , 12 , 32 , 45 ]
print ("intial_list", str (ini_list))
diff_list = []
for x, y in zip (ini_list[ 0 ::], ini_list[ 1 ::]):
diff_list.append(y - x)
print ("difference list : ", str (diff_list))
|
Output:
intial_list [5, 4, 89, 12, 32, 45]
difference list: [-1, 85, -77, 20, 13]
Time Complexity: O(n), where n is the length of the list ini_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the diff list
Method #2: Using Naive approach
Python3
ini_list = [ 5 , 4 , 89 , 12 , 32 , 45 ]
print ("intial_list", str (ini_list))
diff_list = []
for i in range ( 1 , len (ini_list)):
diff_list.append(ini_list[i] - ini_list[i - 1 ])
print ("difference list : ", str (diff_list))
|
Output:
intial_list [5, 4, 89, 12, 32, 45]
difference list: [-1, 85, -77, 20, 13]
Method #3: Using numpy
Python3
import numpy as np
ini_list = np.array([ 5 , 4 , 89 , 12 , 32 , 45 ])
print ("intial_list", str (ini_list))
diff_list = np.diff(ini_list)
print ("difference list : ", str (diff_list))
|
Output:
intial_list [ 5 4 89 12 32 45]
difference list: [ -1 85 -77 20 13]
Method #4: Using list comprehension(one liner)
To calculate the difference between adjacent elements in a list using list comprehension, you can use the following approach:
Python3
ini_list = [ 5 , 4 , 89 , 12 , 32 , 45 ]
diff_list = [ini_list[i + 1 ] - ini_list[i] for i in range ( len (ini_list) - 1 )]
print (diff_list)
|
Output
[-1, 85, -77, 20, 13]
Time complexity: O(n)
Auxiliary Space: O(n)
Using map and lambda:
Approach:
lst[:-1] returns [5, 4, 89, 12, 32], which is lst with the last element removed.
lst[1:] returns [4, 89, 12, 32, 45], which is lst with the first element removed.
The lambda x, y: y – x function takes two arguments x and y and returns their difference y – x.
map(lambda x, y: y – x, lst[:-1], lst[1:]) applies the lambda function to corresponding elements from lst[:-1] and lst[1:], resulting in an iterator object of the form <map object at 0x…>.
list(map(lambda x, y: y – x, lst[:-1], lst[1:])) converts the iterator object to a list, which is the output of diff_adjacent_elements_3.
The resulting list is [-1, 85, -77, 20, 13], which is the difference between adjacent elements in the input list [5, 4, 89, 12, 32, 45].
Python3
def diff_adjacent_elements_3(lst):
return list ( map ( lambda x, y: y - x, lst[: - 1 ], lst[ 1 :]))
initial_list = [ 5 , 4 , 89 , 12 , 32 , 45 ]
diff_list = diff_adjacent_elements_3(initial_list)
print (f "Input list: {initial_list}" )
print (f "Difference list: {diff_list}" )
|
Output
Input list: [5, 4, 89, 12, 32, 45]
Difference list: [-1, 85, -77, 20, 13]
Time Complexity: O(n)
Auxiliary Space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Apr, 2023
Like Article
Save Article