Python | Records list XOR
Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among tuple list elements. This is an essential utility as we come across bitwise operations many times. Let’s discuss certain ways in which XOR can be performed.
Method #1 : Using reduce() + lambda + “^” operator + loop
The above functions can be combined to perform this task. We can first flatten the tuple list using loop and then employ reduce() to accumulate the result of XOR logic specified by the lambda function. Works only with Python2.
Python3
test_list = [( 4 , 6 ), ( 2 , ), ( 3 , 8 , 9 )]
print ( "The original list is : " + str (test_list))
temp = []
for sub in test_list:
for ele in sub:
temp.append(ele)
res = reduce ( lambda x, y: x ^ y, temp)
print ( "The Bitwise XOR of records list elements are : " + str (res))
|
Output :
The original list is : [(4, 6), (2, ), (3, 8, 9)]
The Bitwise XOR of records list elements are : 2
Time Complexity: O(n*n) where n is the number of elements in the string list. The reduce() + lambda + “^” operator + loop is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list.
Method #2 : Using reduce() + operator.ixor + chain()
This task can also be performed using this method. In this the task performed by lambda function in above method is performed using or function for cumulative XOR operation, the flattening of elements to list is performed using chain(). Works with Python2 only.
Python3
from operator import ixor
from itertools import chain
test_list = [( 4 , 6 ), ( 2 , ), ( 3 , 8 , 9 )]
print ( "The original list is : " + str (test_list))
temp = list (chain( * test_list))
res = reduce (ixor, temp)
print ( "The Bitwise XOR of records list elements are : " + str (res))
|
Output :
The original list is : [(4, 6), (2, ), (3, 8, 9)]
The Bitwise XOR of records list elements are : 2
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant space required
Method #3: Using reduce() + lambda
Python3
from functools import reduce
test_list = [( 4 , 6 ), ( 2 , ), ( 3 , 8 , 9 )]
result = reduce ( lambda x, y: x ^ y, [ele for sub in test_list for ele in sub])
print ( "The Bitwise XOR of records list elements are :" , result)
|
Output
The Bitwise XOR of records list elements are : 2
Time complexity: O(n), where n is the total number of elements in the list.
Auxiliary Space: O(1), since only one variable res is used.
Method #4: Using list comprehension + bitwise XOR operator
Use list comprehension to flatten the list of tuples and then use the bitwise XOR operator to calculate the XOR of all elements.
Approach:
- Start by defining a list of tuples called test_list.
- Next, use list comprehension to flatten the list of tuples.
- Now that we have a flat list of integers, then calculate the XOR of all the elements in the list using the bitwise XOR operator ^. We initialize a variable res to the first element in the list.
- Finally, print the result using the print() function.
- Which is the XOR of all the elements in the original test_list.
Python3
test_list = [( 4 , 6 ), ( 2 , ), ( 3 , 8 , 9 )]
temp = [ele for sub in test_list for ele in sub]
res = temp[ 0 ]
for i in range ( 1 , len (temp)):
res ^ = temp[i]
print ( "The Bitwise XOR of records list elements are : " + str (res))
|
Output
The Bitwise XOR of records list elements are : 2
Time complexity: O(n), where n is the total number of elements in the list of tuples.
Auxiliary space: O(n), where n is the total number of elements in the list of tuples.
Method #5: Using itertools.chain.from_iterable() + reduce() + operator.ixor()
This method uses the itertools.chain.from_iterable() function to flatten the list of tuples into a single iterable. The reduce() function is then used with the operator.ixor() function to perform the XOR operation on all the elements.
- Import the necessary modules:
- Initialize the input list:
- Use itertools.chain.from_iterable() to flatten the list of tuples:
- Use reduce() with operator.ixor() to perform the XOR operation on all the elements:
- Print the result.
Python3
from itertools import chain
from functools import reduce
import operator
test_list = [( 4 , 6 ), ( 2 , ), ( 3 , 8 , 9 )]
flattened_list = chain.from_iterable(test_list)
res = reduce (operator.ixor, flattened_list)
print ( "The Bitwise XOR of records list elements are : " + str (res))
|
Output
The Bitwise XOR of records list elements are : 2
Time complexity: O(n), where n is the total number of elements in the list.
Auxiliary space: O(1), as the flattened list is created on the fly and only the result is stored in memory.
Last Updated :
13 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...