Python | Tuple multiplication
Last Updated :
23 Apr, 2023
Sometimes, while working with records, we can have a problem in which we may need to perform multiplication of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using zip() + generator expression The combination of above functions can be used to perform this task. In this, we perform the task of multiplication using generator expression and mapping index of each tuple is done by zip().
Python3
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple (ele1 * ele2 for ele1, ele2 in zip (test_tup1, test_tup2))
print ( "The multiplied tuple : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The multiplied tuple : (50, 24, 35, 30)
Time complexity: O(n), where n is the length of the smaller tuple.
Auxiliary space: O(n), where n is the length of the generated result tuple.
Method #2 : Using map() + mul The combination of above functionalities can also perform this task. In this, we perform the task of extending logic of multiplication using mul and mapping is done by map().
Python3
from operator import mul
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( map (mul, test_tup1, test_tup2))
print ( "The multiplied tuple : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The multiplied tuple : (50, 24, 35, 30)
Time complexity: O(n), where n is the length of the tuples test_tup1 and test_tup2, as the map function iterates through each element of the tuples once.
Auxiliary space: O(n) as well, as it creates a new tuple to store the multiplied result using the map function.
Method #3: Using for loop
Python3
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = []
for i in range ( 0 , len (test_tup1)):
res.append(test_tup1[i] * test_tup2[i])
res = tuple (res)
print ( "The multiplied tuple : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The multiplied tuple : (50, 24, 35, 30)
Method #3: Using numpy
Note: install numpy module using command “pip install numpy”
Python3
import numpy as np
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
arr1 = np.array(test_tup1)
arr2 = np.array(test_tup2)
res = arr1 * arr2
res = tuple (res)
print ( "The multiplied tuple : " + str (res))
|
Output:
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The multiplied tuple : (50, 24, 35, 30)
Time complexity: O(n) where n is the number of elements in the tuple. This is because numpy’s array multiplication is implemented in C and is highly optimized. The Auxiliary space: O(n) as it requires an additional numpy array to store the result.
Method 4 : using a list comprehension.
- Initialize two tuples test_tup1 and test_tup2 with the values (10, 4, 5, 6) and (5, 6, 7, 5), respectively.
- Define a list comprehension [x * y for x, y in zip(test_tup1, test_tup2)] that iterates through both tuples in parallel using the zip function, multiplies the corresponding elements of each tuple, and returns a list of the results.
- Convert the resulting list into a tuple using the tuple function and assign it to the variable res.
- Print the value of res, which is (50, 24, 35, 30).
Python3
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
res = tuple ([x * y for x, y in zip (test_tup1, test_tup2)])
print (res)
|
The time complexity O(n), where n is the length of the tuples.
The space complexity : O(n) .
Similar Reads
Python | Tuple list cross multiplication
Sometimes, while working with Python records, we can have a problem in which we need to perform cross multiplication of list of tuples. This kind of application is popular in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using list comprehension
5 min read
Python - Unique values Multiplication
This article focuses on one of the operation of getting the unique list from a list that contains a possible duplicates and performing its product. This operations has large no. of applications and hence itâs knowledge is good to have. Method 1 : Naive method + loop In naive method, we simply traver
5 min read
Python | Multiplication till Null value
The prefix array is quite famous in the programming world. This article would discuss a variation of this scheme. This deals with the cumulative list product till a False value, and again starts cumulation of product from occurrence of True value. Letâs discuss certain way in which this can be perfo
4 min read
Python | Tuple XOR operation
Sometimes, while working with records, we can have a problem in which we may need to perform mathematical bitwise XOR operation across tuples. This problem can occur in day-day programming. Letâs discuss certain ways in which this task can be performed. Method #1: Using zip() + generator expression
6 min read
Tuple Division in Python
Sometimes, while working with records, we can have a problem in which we may need to perform mathematical division operation across tuples. This problem can occur in day-day programming. Letâs discuss certain ways in which this task can be performed. Method #1 : Using zip() + generator expression Th
6 min read
Multiplication Table Using While Loop in Python
Multiplication tables are fundamental in mathematics, serving as the building blocks for more advanced concepts. In Python, we can use various methods to generate multiplication tables, and one versatile tool for this task is the 'while' loop. In this article, we will explore some commonly used and
3 min read
Python | Addition of tuples
Sometimes, while working with records, we might have a common problem of adding contents of one tuple with the corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Let's discuss certain ways in which this task can be performed. Metho
5 min read
Python - Clearing a tuple
Sometimes, while working with Records data, we can have a problem in which we may require to perform clearing of data records. Tuples, being immutable cannot be modified and hence makes this job tough. Let's discuss certain ways in which this task can be performed. Method #1 : Using list() + clear()
4 min read
tuple() Constructor in Python
In Python, the tuple() constructor is a built-in function that is used to create tuple objects. A tuple is similar to a list, but it is immutable (elements can not be changed after creating tuples). You can use the tuple() constructor to create an empty tuple, or convert an iterable (such as a list,
3 min read
Python | Compare tuples
Sometimes, while working with records, we can have a problem in which we need to check if each element of one tuple is greater/smaller than it's corresponding index in other tuple. This can have many possible applications. Let's discuss certain ways in which this task can be performed. Method #1 : U
4 min read