Finding the Product of Consecutive Pairs in a List


Introduction

In this Article, we will take a look at the python program of finding the product of consecutive pairs in a list. Basically we need to group two elements in which the next element will be at the consecutive position of the first element. After that, each pair of numbers must be multiplied. An example will be used to assist us examine this issue. Understanding this problem with the help of an example: [2,4,6,8,10]. As a result, the consecutive pairs formed will be are (2,4), (4,6), (6,8), (8,10). First step of our program is completed now as we have formed the list of consecutive pairs. Now we will multiply the consecutive pairs, so the final result will calculated easily. So the step by step will be discussed in the section “Understanding the problem”.

What is a List?

List in python can be defined as the collection of different numbers in the single variable. We can understand list with the help of following example. Suppose we have bag. Inside the bag we have kept different different objects. In the same way list works, we put numbers and strings inside the list. list can be used to store many numbers in a single place, it is necessary to use list in python program because it makes the code shorter and easier. Creating different different variables for every number needed to be stored is bad when we see for development point of view. Loops can be used to repeat the task for each and every set of numbers in list.

Understanding the Problem

As we have studied in the introduction section about the example [1, 2, 3, 4, 5]. This is basically a list of 5 numbers. Now we will form the consecutive pair from the above example.so the consecutive pairs will be (1,2), (2,3), (3,4), (4,5) . So in the next step we will be the multiply each pair of elements. The element at the index 1st in this series is 1.when we multiply this number with the number that comes after it, which is 2. So, 1 multiplied by 2 gives us 2. Next, after we moving on to the second number, 2, and multiply it by the next number, 3. This gives us 2 multiplied by 3 equals 6. We will repeat the process until all the elements are completely taken in consideration. multiply 3 by the next number, 4, we get 12, which is the value needed. Similar to this, multiplying 4 by 5 results in the number 20, as 4 times 5 equals 20. The list is now complete, and we have a new list that contains the progeny of these pairs: [2, 6, 12, 20]. After the multiplication of these consecutive pairs, we got a sequence of numbers that is the result of the problem “finding the product of consecutive pairs in a list in python”

Solving the Problem

We will solve the problem with the help of Python language. Here is the step by step procedure of writing program in python for the problem “Finding the Product of Consecutive Pairs in a List” First we will define the function named it as “prod” . inside th is “prod” function there will be an argument named “mylist”. Then we will declare the variable “result”. In this way we are writing instructions for finding the product of consecutive pairs for the interpreter. This variable will behave as the containers for the final answer

For loop we have created after declaring the result variable will help us to iterate the whole list from 0th index to the final index. Inside this “for” loop we have created a formula for the multiplication of the consecutive pairs. The loop will repeat over and over again. In the next line of code update the product variable. In the end we will return the result to the function.

Coming to main code in which we have declared variable named “numbers” and we have initialized five numbers inside it. In the next line we are calling the above “prod” function. Then we will at the position to print the final product. In this we have successfully solved the problem of finding the product of consecutive pairs in a list.

Example

def prod(mylist):
   result = []
   for i in range(len(mylist) - 1):
      product = mylist[i] * mylist[i + 1]
      result.append(product)
   return result
      
numbers = [1, 2, 3, 4, 5]
products = prod(numbers)
print(products)

Output

[2, 6, 12, 20]

Conclusion

Solving these kind of problems, helps us to get better understand of python language. We have created a function with use of keyword “def” which is already defined in python. We have multiplied each element with its next neighbouring one. This problem basically helps us in creating number patterns. These kind of problems increases our problem solving skills. It also improves our understanding and learnings of multiplication.

Updated on: 27-Jul-2023

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements