Change language

# Python | Calculate the difference between adjacent elements in a given list

|

Method # 1: Using `zip()`

` `

` # Python code for demo # calculate the difference # between adjacent elements in the list     # initializing _list ini_list = [ 5 , 4 , 89 , 12 , 32 , 45 ]  < br /> # print iniial_list print ( "intial_list" , str (ini_list))   # Difference calculation diff_list = [] for x, y in zip (ini_list [ 0 ::], ini_list [ 1 ::]):   diff_list.append (y - x)   # print a list of differences print ( " difference list: " , str (diff_list))  `

` ` Exit :

` intial_list [5, 4, 89, 12, 32, 45] difference list: [-1, 85, -77, 20, 13] `

Method # 2: Taking a Naive Approach

 ` # Python code for demo ` ` # calculate the difference ` ` # between adjacent elements in the list ` ` `    ` # initializing _list ` ` ini_list ` ` = ` ` [` ` 5 ` `, ` ` 4 ` `, ` ` 89 ` `, ` ` 12 ` `, ` ` 32 ` `, ` ` 45 ` `] `   ` # print iniial_list ` ` print ` ` (` ` "intial_list" ` `, ` ` str ` ` (ini_list)) `   ` # Difference calculation ` ` diff_list ` ` = ` ` [] `   ` fo r ` ` i ` ` in ` ` range ` ` (` ` 1 ` `, ` ` len ` ` (ini_list)): ` ` diff_list.append (ini_list [i] ` ` - ` ` ini_list [i ` ` - ` ` 1 ` `]) ` ` `  ` # print a list of differences ` ` print ` ` (` `" difference list: "` `, ` ` str ` ` (diff_list)) `

Exit:

` intial_list [5, 4, 89, 12, 32, 45] difference list: [-1, 85, -77, 20, 13] `

Method # 3: Using NumPy

 ` # Python code for demo ` ` # calculate the difference ` ` # between adjacent elements in the list ` ` `  ` import ` ` numpy as np ` ` # initializing _list ` ` ini_list ` ` = ` ` np .array ([` ` 5 ` `, ` ` 4 ` `, ` ` 89 ` `, ` ` 12 ` `, ` ` 32 ` `, ` ` 45 ` `]) ` ` `  ` # print iniial_list ` ` print ` ` (` ` "intial_list" ` `, ` ` str ` ` (ini_list)) `   ` # Difference calculation ` ` diff_list ` ` = ` ` np. diff (ini_list) `   ` # print a list of differences ` ` print ` ` (` ` "difference list:" ` `, ` ` str ` ` (diff_list)) `

Exit :

` intial_list [5 4 89 12 32 45] difference list: [-1 85 - 77 20 13] `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries