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 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)) ` |

*chevron_right*

*filter_none*

**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 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)) ` |

*chevron_right*

*filter_none*

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 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)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list is : [1, 4, 5, 3, 6] The computed successive difference list is : [3, 1, -2, 3]

## Recommended Posts:

- Python - Successive element pairing
- Python | Find the equivalent discount in successive discounts in percentages
- Python - Successive Characters Frequency
- Python - Extract Strings with Successive Alphabets in Alphabetical Order
- Generate a list using given frequency list
- Python | Generate random numbers within a given range and store in a list
- Python | Generate Personalized Data from given list of expressions
- Python - Generate random number except K in list
- Python | Generate random number except K in list
- Python program to generate a list of alphabets in lexical order
- Python | Slicing list from Kth element to last element
- Python | Insert Nth element to Kth element in other list
- Python | Replace elements in second list with index of same element in first list
- Python | Move one list element to another list
- Python | Indices list of matching element from other list
- Python | Subtract two list elements if element in first list is greater
- Python program to get the indices of each element of one list in another list
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List

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.