Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Multiply elements of Tuple

Python | Multiply elements of Tuple

Given, a list of tuples, the task is to multiply the elements of the tuple and return a list of the multiplied elements.

Examples:

Input: [(2, 3), (4, 5), (6, 7), (2, 8)] 
Output: [6, 20, 42, 16] 

Input: [(11, 22), (33, 55), (55, 77), (11, 44)] 
Output: [242, 1815, 4235, 484]

There are multiple ways to multiply the elements of a tuple. Let’s see a couple of them. 

Method 1: Using Iteration This is the most naive method to achieve a solution to this task. In this, we iterate over the whole list of tuples and multiply the elements in each tuple to get the list of elements. 

Python3




# Python code to convert list of tuple into list of elements
# formed by multiplying elements of tuple.
 
# Input list initialisation
Input = [(2, 3), (4, 5), (6, 7), (2, 8)]
 
# Output list initialisation
Output = []
 
# Iteration to multiply element and append multiplied element in
# new list
for elem in Input:
    temp = elem[0]*elem[1]
    Output.append(temp)
 
# printing output
print("The original list of tuple is ")
print(Input)
 
print("\nThe answer is")
print(Output)


Output: 

The original list of tuple is 
[(2, 3), (4, 5), (6, 7), (2, 8)]

The answer is
[6, 20, 42, 16]

# Method 2: Using list comprehension This is the one-liner approach to achieve the solution to this task. 

Python3




# Python code to convert list of tuple into list of elements
# formed by multiplying elements of tuple.
 
# Input list initialisation
Input = [(2, 3), (4, 5), (6, 7), (2, 8)]
 
# Iteration to multiply element and append multiplied element in
# new list
Output = [(x * y) for x, y in Input]
 
# printing output
print("The original list of tuple is ")
print(Input)
 
print("\nThe answer is")
print(Output)


Output:

The original list of tuple is 
[(2, 3), (4, 5), (6, 7), (2, 8)]

The answer is
[6, 20, 42, 16]

Method 3: Using map() and lambda function
In this method, we use the map() function along with a lambda function to multiply the elements of each tuple.

Python3




#Python code to convert list of tuple into list of elements
#formed by multiplying elements of tuple.
#Input list initialisation
Input = [(2, 3), (4, 5), (6, 7), (2, 8)]
 
#Iteration to multiply element and append multiplied element in
#new list
Output = list(map(lambda x: x[0]*x[1], Input))
 
#printing output
print("The original list of tuple is ")
print(Input)
 
print("\nThe answer is")
print(Output)
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list of tuple is 
[(2, 3), (4, 5), (6, 7), (2, 8)]

The answer is
[6, 20, 42, 16]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using reduce()

Use the reduce() function from the functools module to apply a lambda function to each tuple in the input list. The lambda function multiplies the two elements of the tuple, and reduce() applies this function to each tuple and returns a single value.

  • Import the reduce() function from the functools module.
  • Initialize the input list of tuples.
  • Define a lambda function to multiply the elements of a tuple.
  • Apply the lambda function to each tuple in the input list using reduce() function and append the result to the output list using a list comprehension.
  • Print the original list of tuples.
  • Print the list of multiplied elements.

Below is the implementation of the above approach:

Python3




# Import the reduce function from functools module
from functools import reduce
 
# Initialize the input list of tuples
Input = [(2, 3), (4, 5), (6, 7), (2, 8)]
 
# Define a lambda function to multiply the elements of a tuple
multiply = lambda x, y: x * y
 
# Apply the lambda function to each tuple in the input list using reduce function
# and append the result to the output list using a list comprehension
Output = [reduce(multiply, tpl) for tpl in Input]
 
# Print the original list of tuples
print("The original list of tuple is ")
print(Input)
 
# Print the list of multiplied elements
print("\nThe answer is")
print(Output)


Output

The original list of tuple is 
[(2, 3), (4, 5), (6, 7), (2, 8)]

The answer is
[6, 20, 42, 16]

Time Complexity: O(n*k), where n is the number of tuples in the input list and k is the number of elements in each tuple.
Auxiliary Space: O(n*k)

RELATED ARTICLES

Most Popular

Recent Comments