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.
# Python code to demonstrate working of # List XOR # Using reduce() + lambda + "^" operator # initializing list test_list = [4, 6, 2, 3, 8, 9] # printing original list print("The original list is : " + str(test_list)) # List XOR # Using reduce() + lambda + "^" operator res = 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.ixor from operator import ixor # initializing list test_list = [4, 6, 2, 3, 8, 9] # printing original list print("The original list is : " + str(test_list)) # List XOR # Using reduce() + operator.ixor res = 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
Recommended Posts:
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert mixed data types tuple list to string list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Sorting list of lists with similar list elements
- Python | Filter list of strings based on the substring list
- Python | Find maximum length sub-list in a nested list
- Python | Merge list of tuple into list by joining the strings
- Python | Convert list of numerical string to list of Integers
- Python | Convert list of string into sorted list of integer
- Python | Subtract two list elements if element in first list is greater
- Python | Merge List with common elements in a List of Lists
- Python | Pair and combine nested list to tuple list
- Python | Replace elements in second list with index of same element in first list
- Python Program to convert List of Integer to List of String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


