Open In App

Python | Records list XOR

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




# Python code to demonstrate working of
# Records list XOR
# Using reduce() + lambda + "^" operator + loops
 
# initializing list
test_list = [(4, 6), (2, ), (3, 8, 9)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Records list XOR
# Using reduce() + lambda + "^" operator + loops
temp = []
for sub in test_list:
   for ele in sub:
       temp.append(ele)
res = reduce(lambda x, y: x ^ y, temp)
 
# printing result
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




# Python code to demonstrate working of
# Records list XOR
# Using reduce() + operator.ixor
from operator import ixor
from itertools import chain
 
# initializing list
test_list = [(4, 6), (2, ), (3, 8, 9)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Records list XOR
# Using reduce() + operator.ixor
temp = list(chain(*test_list))
res = reduce(ixor, temp)
 
# printing result
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
# initializing list
 
test_list = [(4, 6), (2, ), (3, 8, 9)]
# Records list XOR
 
result = reduce(lambda x, y: x ^ y, [ele for sub in test_list for ele in sub])
# printing result
 
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)]
 
# flatten the list of tuples using list comprehension
temp = [ele for sub in test_list for ele in sub]
 
# calculate the XOR of all elements using the bitwise XOR operator
res = temp[0]
for i in range(1, len(temp)):
    res ^= temp[i]
 
# print the result
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.

  1. Import the necessary modules:
  2. Initialize the input list:
  3. Use itertools.chain.from_iterable() to flatten the list of tuples:
  4. Use reduce() with operator.ixor() to perform the XOR operation on all the elements:
  5. Print the result.

Python3




from itertools import chain
from functools import reduce
import operator
 
# initializing list
test_list = [(4, 6), (2, ), (3, 8, 9)]
 
# Using itertools.chain.from_iterable() + reduce() + operator.ixor()
flattened_list = chain.from_iterable(test_list)
res = reduce(operator.ixor, flattened_list)
 
# printing result
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
Previous
Next
Share your thoughts in the comments
Similar Reads