65. Problem to get the difference between consecutive numbers in list.

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
  1. Take a list as input.
  2. Combine the elements from the list using the zip() function.
  3. Find the difference between the consecutive numbers in the list.
  4. Add that difference to a new list.
  5. 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

Extract strings of specified sizes from the list

Calculate the average of the list

Leave a Comment