Python: Find the difference between consecutive numbers in a given list
Last update on December 10 2020 11:24:43 (UTC/GMT +8 hours)Python | Calculate difference between adjacent elements in given list
Given a list, the task is to create a new list containing difference of adjacent elements in the given list.
Method #1: Using zip()
# Python code to demonstrate
# to calculate difference
# between adjacent elements in list
# initialising _list
ini_list = [5, 4, 89, 12, 32, 45]
# printing iniial_list
print("intial_list", str(ini_list))
# Calculating difference list
diff_list = []
for x, y in zip(ini_list[0::], ini_list[1::]):
diff_list.append(y-x)
# printing difference list
print ("difference list: ", str(diff_list))
|
Method #2: Using Naive approach
# Python code to demonstrate
# to calculate difference
# between adjacent elements in list
# initialising _list
ini_list = [5, 4, 89, 12, 32, 45]
# printing iniial_list
print("intial_list", str(ini_list))
# Calculating difference list
diff_list = []
for i in range(1, len(ini_list)):
diff_list.append(ini_list[i] - ini_list[i-1])
# printing difference list
print ("difference list: ", str(diff_list))
|
intial_list [5, 4, 89, 12, 32, 45] difference list: [-1, 85, -77, 20, 13]
Method #3: Using numpy
# Python code to demonstrate
# to calculate difference
# between adjacent elements in list
import numpy as np
# initialising _list
ini_list = np.array([5, 4, 89, 12, 32, 45])
# printing iniial_list
print("intial_list", str(ini_list))
# Calculating difference list
diff_list = np.diff(ini_list)
# printing difference list
print ("difference list: ", str(diff_list))
|
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Python | Generate successive element difference list
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))
|
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))
|
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))
|
Output :
The original list is : [1, 4, 5, 3, 6] The computed successive difference list is : [3, 1, -2, 3]Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
“python difference between consecutive element in list” Code Answer
Python answers related to “python difference between consecutive element in list”
- 2 list difference python
- python consecutive numbers difference between
- find number of common element in two python array
- python common elements in two arrays
- count consecutive values in python
- find matches between two lists python
- find different values from two lists python
- python list of difference beetwen values in list
- find common elements in two lists python
- find different between list
- calculate the same value in list i python
Python queries related to “python difference between consecutive element in list”
- python list difference between consecutive elements
- python difference between consecutive element in list
- python find consecutive elements in list
- difference between consecutive elements in list python
- how get difference between two consecutive elements in a list
- compare consecutive elements in list python
- python find difference between consecutive elements in list
- how to take the difference of consecutive numbers in list python
- difference of two consecutive elements in array python
- python find two consecutive numbers in list
- python list difference between consecutive elements using if stament
- extracting consecutive elements from a list python
- get the difference between every consecutive element in a list
- how to compare consecutive elements in list python
- get two consecutive elements from list python
- python difference between consecutive elements in list
- difference between consecutive numbers in list python
- python list difference between consecutive elements using if
- print the difference between consecutive elements in array python
- find difference between consecutive elements in list python
- python consecutive difference in list
- python diff between consecutive elements in list
“python diff between consecutive elements in list” Code Answer
Python answers related to “python diff between consecutive elements in list”
- elementwise comparison list python
- 2 list difference python
- python consecutive numbers difference between
- python common elements in two arrays
- count consecutive values in python
- find matches between two lists python
- find different values from two lists python
- python list of difference beetwen values in list
- find common elements in two lists python
- find different between list
Python queries related to “python diff between consecutive elements in list”
- python list difference between consecutive elements
- python difference between consecutive element in list
- python find consecutive elements in list
- difference between consecutive elements in list python
- how get difference between two consecutive elements in a list
- compare consecutive elements in list python
- python find difference between consecutive elements in list
- how to take the difference of consecutive numbers in list python
- difference of two consecutive elements in array python
- python find two consecutive numbers in list
- python list difference between consecutive elements using if stament
- extracting consecutive elements from a list python
- get the difference between every consecutive element in a list
- how to compare consecutive elements in list python
- get two consecutive elements from list python
- python difference between consecutive elements in list
- difference between consecutive numbers in list python
- python list difference between consecutive elements using if
- print the difference between consecutive elements in array python
- find difference between consecutive elements in list python
- python consecutive difference in list
- python diff between consecutive elements in list
Program to find numbers with same consecutive differences in Python
Suppose we have to find an array of size N such that the absolute difference between every two consecutive digits is K. Every number in the answer must not have leading zeros except for the number 0 itself.
So, if the input is like N = 4 K = 7, then the output will be [1818, 2929, 7070, 8181, 9292], here 0707 is not valid as it has leading 0.
To solve this, we will follow these steps −
if N is same as 1, then
return a new list from range 0 to 9
queue := make a queue with all elements from 1 to 9
for n in range 0 to N - 2, do
len_queue := size of queue
for j in range 0 to len_queue - 1, do
num := left item of queue, and delete it from queue
lsd := num mod 10
if lsd - K >= 0, then
insert num * 10 + lsd - K at the end of queue
if K and lsd + K <= 9, then
insert num * 10 + lsd + K at the end of queue
return elements of queue