Python – Multiply Consecutive elements in 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 product 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Consecutive Product 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
# Consecutive Product list
res = [test_list[i] * test_list[i + 1] for i in range(len(test_list)-1)]
  
# printing result
print ("The computed successive product list is : " + str(res))

chevron_right


Output :

The original list is : [1, 4, 5, 3, 6]
The computed successive product list is : [4, 20, 15, 18]

 

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 product.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Consecutive Product 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()
# Consecutive Product list
res = [i * j for i, j in zip(test_list[: -1], test_list[1 :])]
  
# printing result
print ("The computed successive product list is : " + str(res))

chevron_right


Output :

The original list is : [1, 4, 5, 3, 6]
The computed successive product list is : [4, 20, 15, 18]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.