Python | Tuple XOR operation
Last Updated :
28 Apr, 2023
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 The combination of above functions can be used to perform this task. In this, we perform the task of XOR using generator expression and mapping index of each tuple is done by zip().
Python3
# Python3 code to demonstrate working of
# Tuple XOR operation
# using zip() + generator expression
# initialize tuples
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple XOR operation
# using zip() + generator expression
res = tuple(ele1 ^ ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
# printing result
print("The XOR tuple : " + str(res))
Output : The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
Time complexity: O(n), where n is the length of the tuples. The XOR operation between two elements takes O(1) time and is performed n times, so the overall time complexity is O(n).
Auxiliary Space: O(n), where n is the length of the result tuple. The generator expression creates a new tuple with the XORed elements, so the space complexity is proportional to the size of the result tuple.
Method #2: Using map() + xor The combination of above functionalities can also perform this task. In this, we perform the task of extending logic of XOR using xor and mapping is done by map().
Python3
# Python3 code to demonstrate working of
# Tuple XOR operation
# using map() + xor
from operator import xor
# initialize tuples
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple XOR operation
# using map() + xor
res = tuple(map(xor, test_tup1, test_tup2))
# printing result
print("The XOR tuple : " + str(res))
Output : The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
Time complexity: O(n), where n is the length of the tuples. This is because the map() function and the xor operator iterate through the tuples once.
Auxiliary space: O(n), where n is the length of the tuples. This is because the tuple created by the map() function is of the same length as the input tuples.
Method #3 : Using numpy
Note: install numpy module using command "pip install numpy"
Python3
# Python3 code to demonstrate working of
# Tuple XOR operation
# using numpy
import numpy as np
# initialize tuples
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple XOR operation
# using numpy
res = np.bitwise_xor(test_tup1,test_tup2)
# printing result
print("The XOR tuple : " + str(tuple(res)))
Output:
The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using for loops
Python3
# Python3 code to demonstrate working of
# Tuple XOR operation
# initialize tuples
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple XOR operation
res=[]
for i in range(0,len(test_tup1)):
res.append(test_tup1[i]^test_tup2[i])
res=tuple(res)
# printing result
print("The XOR tuple : " + str(res))
OutputThe original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
Time complexity: O(n)
Auxiliary Space: O(n)
Method 5: Using List Comprehension
Use list comprehension to iterate over the elements of both tuples and perform the XOR operation. The resulting list can then be converted to a tuple.
Below is the implementation of the above idea:
Python3
# initialize tuples
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# perform XOR operation using list comprehension
res = tuple([test_tup1[i] ^ test_tup2[i] for i in range(len(test_tup1))])
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# print the result
print("The XOR tuple : " + str(res))
OutputThe original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
Time complexity: O(n), where n is the length of the tuples test_tup1 and test_tup2.
Auxiliary Space: O(n)
Method #6: Using itertools.starmap() + operator.xor()
The itertools.starmap() function applies a function to a sequence of argument tuples, and operator.xor() function returns the bitwise XOR of two integers.
Python3
import operator
import itertools
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# Tuple XOR operation using itertools.starmap() and operator.xor()
res = tuple(itertools.starmap(operator.xor, zip(test_tup1, test_tup2)))
# printing result
print("The XOR tuple : " + str(res))
OutputThe XOR tuple : (15, 6, 5, 10)
Time complexity: O(n), where n is the length of the tuples test_tup1 and test_tup2.
Auxiliary Space: O(n)
Method #7:Using the pandas module
- Import the pandas module and initialize two tuples.
- Create two data frames using the pandas.DataFrame() function, one for each tuple.
- Use the pandas.DataFrame.diff() function to calculate the difference between the two data frames.
- Use the pandas.DataFrame.gt() function to create a boolean mask indicating where the values in the first data frame are greater than those in the second data frame.
- Use the pandas.DataFrame.where() function to apply the boolean mask to the results of step 3.
- Use the pandas.DataFrame.fillna() function to fill any NaN values in the resulting data frame with False.
- Convert the resulting data frame to a list of lists using the pandas.DataFrame.values.tolist() function.
Python3
import pandas as pd
# initialize tuples
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# create pandas DataFrames
df1 = pd.DataFrame(list(test_tup1)).T
df2 = pd.DataFrame(list(test_tup2)).T
# perform XOR operation
res_df = df1.astype(int).apply(lambda x: x^df2.astype(int).iloc[0], axis=1)
# convert result DataFrame to tuple
res = tuple(res_df.iloc[0].tolist())
# print result
print("The XOR tuple : ", res)
Output-
The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The XOR tuple : (15, 6, 5, 10)
The time complexity of this solution is O(n), where n is the length of the tuple.
The space complexity of this solution is O(n), as we are creating a new tuple of length n to store the result.
Similar Reads
Python - AND operation between Tuples 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 w
4 min read
Python | Tuple multiplication 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 abov
5 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,
2 min read