Open In App

Python | Generate successive element difference list

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

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




# Python3 code to demonstrate
# to generate successive difference list
# using list comprehension
 
# initializing list
test_list = [1, 4, 5, 3, 6]
 
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using list comprehension
# generate successive difference list
res = [test_list[i + 1] - test_list[i] for i in range(len(test_list)-1)]
 
# printing result
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




# Python3 code to demonstrate
# to generate successive difference list
# using zip()
 
# initializing list
test_list = [1, 4, 5, 3, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using zip()
# generate successive difference list
res = [j - i for i, j in zip(test_list[: -1], test_list[1 :])]
 
# printing result
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




# Python3 code to demonstrate
# to generate successive difference list
# using map() + operator.sub
import operator
 
# initializing list
test_list = [1, 4, 5, 3, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using map() + operator.sub
# generate successive difference list
res = list(map(operator.sub, test_list[1:], test_list[:-1]))
 
# printing result
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
 
# Initializing list
test_list = [1, 4, 5, 3, 6]
 
# Printing original list
print("The original list is:", test_list)
 
# Using NumPy's diff function to compute successive difference list
res = np.diff(test_list)
 
# Printing result
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:

  1. Define a function successive_difference that takes in a list test_list as input.
  2. If the length of test_list is less than or equal to 1, return an empty list.
  3. 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




# Python3 code to demonstrate
# to generate successive difference list
def successive_difference(test_list):
    if len(test_list) <= 1:
        return []
    else:
        return [test_list[1] - test_list[0]] + successive_difference(test_list[1:])
 
# initializing list
test_list = [1, 4, 5, 3, 6]
 
 
# printing original list
print ("The original list is : " + str(test_list))
 
# generate successive difference list
res = successive_difference(test_list)
# printing result
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]

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




# Python code to demonstrate
# to generate successive difference list
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
  
# initializing list
test_list = [1, 4, 5, 3, 6]
  
# printing original list
print("The original list is:", test_list)
  
# generate successive difference list
res = successive_difference(test_list)
  
# printing result
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)



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

Similar Reads