While working with python, we usually come by many problems that we need to solve in day-day and in development. Specially in development, small tasks of python are desired to be performed in just one line. We discuss some ways to compute a list consisting of elements that are successive difference in the list.
Method #1 : Using list comprehension Naive method can be used to perform, but as this article discusses the one liner solutions to this particular problem, we start with the list comprehension as a method to perform this task.
Python3
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ("The original list is : " + str (test_list))
res = [test_list[i + 1 ] - test_list[i] for i in range ( len (test_list) - 1 )]
print ("The computed successive difference list is : " + str (res))
|
Output :
The original list is : [1, 4, 5, 3, 6]
The computed successive difference list is : [3, 1, -2, 3]
Method #2 : Using zip() zip() can also be used to perform the similar task and uses the power of negative indices to zip() the index element with its next element and hence compute the difference.
Python3
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ("The original list is : " + str (test_list))
res = [j - i for i, j in zip (test_list[: - 1 ], test_list[ 1 :])]
print ("The computed successive difference list is : " + str (res))
|
Output :
The original list is : [1, 4, 5, 3, 6]
The computed successive difference list is : [3, 1, -2, 3]
Method #3 : Using map() + operator.sub map() can be coupled with the subtraction operator to perform this particular task. This maps the element with its next element and performs the subtraction operation. Other operators can be passed to perform the desired operations.
Python3
import operator
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ("The original list is : " + str (test_list))
res = list ( map (operator.sub, test_list[ 1 :], test_list[: - 1 ]))
print ("The computed successive difference list is : " + str (res))
|
Output :
The original list is : [1, 4, 5, 3, 6]
The computed successive difference list is : [3, 1, -2, 3]
Method #4 :
To install NumPy, you can use the following command:
pip install numpy
One more approach to generate the successive difference list is to use the numpy library. We can use the numpy.diff function to compute the difference between elements in an array or list.
Here is an example of using numpy to generate the successive difference list:
Python3
import numpy as np
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ( "The original list is:" , test_list)
res = np.diff(test_list)
print ( "The computed successive difference list is:" , res)
|
Output:
The original list is: [1, 4, 5, 3, 6]
The computed successive difference list is: [ 3 1 -2 3]
Time complexity: O(n)
Auxiliary Space: O(n)
Method#5: Using Recursive method.
Algorithm:
- Define a function successive_difference that takes in a list test_list as input.
- If the length of test_list is less than or equal to 1, return an empty list.
- Otherwise, return a list that contains the difference between the second element and the first element of test_list, concatenated with the result of calling successive_difference on the list obtained by removing the first element of test_list.
Python3
def successive_difference(test_list):
if len (test_list) < = 1 :
return []
else :
return [test_list[ 1 ] - test_list[ 0 ]] + successive_difference(test_list[ 1 :])
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ( "The original list is : " + str (test_list))
res = successive_difference(test_list)
print ( "The computed successive difference list is : " + str (res))
|
OutputThe original list is : [1, 4, 5, 3, 6]
The computed successive difference list is : [3, 1, -2, 3]
Time complexity: O(n), where n is the length of test_list.
Auxiliary Space: O(n), where n is the length of test_list, due to the space used by the recursive call stack.
Method: Using an iterative approach:
Algorithm:
Initialize an empty list res to store the computed successive difference list.
Iterate through the list using a for loop from index 1 to the end of the list.
Compute the difference between the current element and the previous element using the formula test_list[i] – test_list[i-1].
Append the computed difference to the res list.
Print the original list and the computed successive difference list.
Python3
def successive_difference(test_list):
res = []
for i in range ( 1 , len (test_list)):
res.append(test_list[i] - test_list[i - 1 ])
return res
test_list = [ 1 , 4 , 5 , 3 , 6 ]
print ( "The original list is:" , test_list)
res = successive_difference(test_list)
print ( "The computed successive difference list is:" , res)
|
OutputThe original list is: [1, 4, 5, 3, 6]
The computed successive difference list is: [3, 1, -2, 3]
Time complexity: O(n)
Auxiliary space: O(n)