Python – List XOR
Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among 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
The above functions can be combined to perform this task. We can employ reduce() to accumulate the result of XOR logic specified by the lambda function. Works only with Python2.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
# Python code to demonstrate working of# List XOR# Using reduce() + lambda + "^" operator # initializing listtest_list = [4, 6, 2, 3, 8, 9] # printing original listprint("The original list is : " + str(test_list)) # List XOR# Using reduce() + lambda + "^" operatorres = reduce(lambda x, y: x ^ y, test_list) # printing result print("The Bitwise XOR of list elements are : " + str(res)) |
The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2
Method #2 : Using reduce() + operator.ixor
This task can also be performed using this method. In this the task performed by lambda function in above method is performed using ior function for cumulative XOR operation. Works with Python2 only.
# Python code to demonstrate working of# List XOR# Using reduce() + operator.ixorfrom operator import ixor # initializing listtest_list = [4, 6, 2, 3, 8, 9] # printing original listprint("The original list is : " + str(test_list)) # List XOR# Using reduce() + operator.ixorres = reduce(ixor, test_list) # printing result print("The Bitwise XOR of list elements are : " + str(res)) |
The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2


