- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Calculate difference between adjacent elements in given list using Python

In this article we will see how we create a new list from a given list by subtracting the values in the adjacent elements of the list. We have various approaches to do that.

## With append and range

In this approach we iterate through list elements by subtracting the values using their index positions and appending the result of each subtraction to a new list. We use the range and len function to keep track of how many iterations to do.

## Example

listA= [25, 97, 13, 62, 14, 102] print("Given list:\n",listA) list_with_diff = [] for n in range(1, len(listA)): list_with_diff.append(listA[n] - listA[n-1]) print("Difference between adjacent elements in the list: \n", list_with_diff)

## Output

Running the above code gives us the following result −

Given list: [25, 97, 13, 62, 14, 102] Difference between adjacent elements in the list: [72, -84, 49, -48, 88]

## With zip and list slicing

In the next approach we create a for loop to find the difference between the adjacent elements and keep appending the result to a new list.

## Example

listA= [25, 97, 13, 62, 14, 102] print("Given list:\n",listA) list_with_diff = [] for i, j in zip(listA[0::], listA[1::]): list_with_diff.append(j - i) print("Difference between adjacent elements in the list: \n", list_with_diff)

## Output

Running the above code gives us the following result −

Given list: [25, 97, 13, 62, 14, 102] Difference between adjacent elements in the list: [72, -84, 49, -48, 88]

- Related Articles
- Python – Adjacent elements in List
- Multiply Adjacent elements in Python
- Program to find sum of non-adjacent elements in a circular list in python
- Program to find minimum possible difference of indices of adjacent elements in Python
- Program to find largest sum of non-adjacent elements of a list in Python
- Python - Joining only adjacent words in list
- Maximum sum of difference of adjacent elements in C++
- Get last N elements from given list in Python
- Python – Calculate the percentage of positive elements of the list
- Maximum length subarray with difference between adjacent elements as either 0 or 1 in C++
- Maximum length subsequence with difference between adjacent elements as either 0 or 1 in C++
- Difference between List and Tuples in Python.
- Difference Between List and Tuple in Python
- Python - Ways to format elements of given list
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?

Advertisements