Python – AND operation between Tuples
Last Updated :
23 Apr, 2023
Sometimes, while working with records, we might have a common problem of performing AND operation contents of one tuple with corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records especially Data Science. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using map() + lambda Combination of above functionalities can solve the problem for us. In this, we compute the AND using lambda functions and extend the logic to keys using map().
Python3
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 2 , 5 , 18 )
print ("The original tuple 1 : " + str (test_tup1))
print ("The original tuple 2 : " + str (test_tup2))
res = tuple ( map ( lambda i, j: i & j, test_tup1, test_tup2))
print ("Resultant tuple after AND operation : " + str (res))
|
Output :
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after AND operation : (2, 4, 0)
Method #2 : Using map() + iand() The combination of above functions can help us achieve this task. In this, we first extend the logic to all using map() and then perform AND of each index using iand().
Python3
import operator
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 2 , 5 , 18 )
print ("The original tuple 1 : " + str (test_tup1))
print ("The original tuple 2 : " + str (test_tup2))
res = tuple ( map (operator.iand, test_tup1, test_tup2))
print ("Resultant tuple after AND operation : " + str (res))
|
Output :
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after AND operation : (2, 4, 0)
Method #3: Using List comprehension
This approach is similar to the previous ones but uses a more concise and readable syntax using list comprehension.
Python3
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 2 , 5 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ([i & j for i,j in zip (test_tup1, test_tup2)])
print ( "Resultant tuple after AND operation : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after AND operation : (2, 4, 0)
Time complexity: O(n) where n is the length of the tuples
Auxiliary Space: O(n) as we are creating a new tuple of the same size as the input tuples
Method #4: Using zip():
Python3
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 2 , 5 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ([i & j for i, j in zip (test_tup1, test_tup2)])
print (res)
|
Output
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
(2, 4, 0)
Time complexity: O(n)
Space complexity: O(n)
Method #5: Using a for loop
Step-by-step approach:
- Initialize an empty list called result.
- Iterate over the indices of the input tuples using the range() function.
- Compute the bitwise AND of the corresponding elements of the input tuples using the & operator.
- Append the result to the result list.
- Convert the result list to a tuple using the tuple() function.
- Return the tuple.
Python3
def bitwise_and_tuples(tup1, tup2):
result = []
for i in range ( len (tup1)):
result.append(tup1[i] & tup2[i])
return tuple (result)
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 2 , 5 , 18 )
res = bitwise_and_tuples(test_tup1, test_tup2)
print (res)
|
Time complexity: O(n), where n is the length of the input tuples.
Auxiliary space: O(n), where n is the length of the input tuples.
Please Login to comment...