Python | Multiply Adjacent elements
Last Updated :
23 Apr, 2023
Sometimes, while working with data, we can have a problem in which we need to find cumulative result. This can be of any type, product or summation. Here we are gonna discuss about adjacent element multiplication. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using zip() + generator expression + tuple() The combination of above functionalities can be used to perform this task. In this, we use generator expression to provide multiplication logic and simultaneous element pairing is done by zip(). The result is converted to tuple form using tuple().
Python3
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ( "The original tuple : " + str (test_tup))
res = tuple (i * j for i, j in zip (test_tup, test_tup[ 1 :]))
print ( "Resultant tuple after multiplication : " + str (res))
|
Output
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after multiplication : (5, 35, 56, 80)
Method #2 : Using tuple() + map() + lambda The combination of above functions can also help to perform this task. In this, we perform multiplication and binding logic using lambda function. The map() is used to iterate to each element and at end result is converted by tuple().
Python3
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ( "The original tuple : " + str (test_tup))
res = tuple ( map ( lambda i, j : i * j, test_tup[ 1 :], test_tup[: - 1 ]))
print ( "Resultant tuple after multiplication : " + str (res))
|
Output
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after multiplication : (5, 35, 56, 80)
Method #4: Using numpy
Note: Install numpy module using command “pip install numpy”
The numpy library in Python provides a function called numpy.multiply() which can be used to perform element-wise multiplication.
Python3
import numpy as np
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ( "The original tuple : " + str (test_tup))
res = np.multiply(test_tup[ 1 :], test_tup[: - 1 ])
print ( "Resultant tuple after multiplication : " + str ( tuple (res)))
|
Output:
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after multiplication : (5, 35, 56, 80)
Time complexity: O(n) where n is the size of the tuple.
Auxiliary Space: O(n)
Method 4: Using a for loop
Step-by-step approach:
- Initialize the input tuple test_tup.
- Print the original tuple.
- Initialize an empty list res to store the result.
- Iterate over the input tuple using a for loop.
- Multiply the current element with its adjacent element and append the result to res.
- Convert res to a tuple using the tuple() function.
- Print the resultant tuple
Python3
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ( "The original tuple : " + str (test_tup))
res = []
for i in range ( len (test_tup) - 1 ):
res.append(test_tup[i] * test_tup[i + 1 ])
res = tuple (res)
print ( "Resultant tuple after multiplication : " + str (res))
|
Output
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after multiplication : (5, 35, 56, 80)
Time complexity: O(n), where n is the length of the input tuple.
Auxiliary space: O(n), to store the res list.
Please Login to comment...