In this Python list program, we will take a user input as a list and find the difference between consecutive numbers in a given list with the help of the below-given steps.
Difference between consecutive numbers in list:
Steps to solve the program
- Take a list as input.
- Combine the elements from the list using the zip() function.
- Find the difference between the consecutive numbers in the list.
- Add that difference to a new list.
- Print the list to see the output.
#Input list
list1 = [1, 1, 3, 4, 4, 5, 6, 7]
#Creating empty list
list2 = []
for a,b in zip(list1[:-1],list1[1:]):
difference = b-a
list2.append(difference)
#Printing output
print(list2)
Output :
[0, 2, 1, 0, 1, 1, 1]
Related Articles
Python program to calculate the average of the given list.
Python program to count integers in a given mixed list.
Python program to access multiple elements of the specified index from a given list.
Python program to check whether a specified list is sorted or not.
Python program to remove duplicate dictionaries from a given list.
Python program to check if the elements of a given list are unique or not.