NumPy: How to Calculate the Difference Between Neighboring Elements in Array Last Updated : 15 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report To calculate the difference between neighboring elements in an array using the NumPy library we use numpy.diff() method of NumPy library. It is used to find the n-th discrete difference along the given axis. The first output is given by: difference[i] = a[i+1] - a[i]Example:Python NumPy program to calculate differences between neighboring elements in a 2d NumPy array Python3 # import library import numpy as np # create a numpy 2d-array arr = np.array([[10, 12, 14], [25, 35, 45], [12, 18, 20]]) # finding the difference between # neighboring elements along column result = np.diff(arr, axis = 0) print(result) Output: [[ 15 23 31][-13 -17 -25]]SyntaxSyntax: numpy.diff(a, n=1, axis=-1, prepend=<no value>, append=<no value>) Parameters a: Input arrayn: The number of times values are differenced.axis: The axis along which the difference is taken, default is the last axis.prepend, append: Values to prepend or append to a along axis prior to performing the difference.Returns: returns the n-th differences Let's check some examples of how to calculate the difference between neighboring elements in an array using NumPy to get a better understanding: More ExamplesLet's look at examples for 1D and 2D arrays: Calculating Differences Between Consecutive Elements in a 1D Numpy Array Python3 # import library import numpy as np # create a numpy 1d-array arr = np.array([1, 12, 3, 14, 5, 16, 7, 18, 9, 110]) # finding the difference between # neighboring elements result = np.diff(arr) print(result) Output: [ 11 -9 11 -9 11 -9 11 -9 101]Calculating Differences Between Neighboring Elements Along Rows in a 2D NumPy Array Python3 # import library import numpy as np # create a numpy 2d-array arr = np.array([[10, 12, 14], [25, 35, 45], [12, 18, 20]]) # finding the difference between # neighboring elements along row result = np.diff(arr, axis = 1) print(result) Output: [[ 2 2][10 10][ 6 2]] Create Quiz Comment V vasu_gupta Follow 1 Improve V vasu_gupta Follow 1 Improve Article Tags : Python Python-numpy Python numpy-program Python numpy-Mathematical Function Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like