Xor all elements in list python

In Python 3 you can use:

>>> from functools import reduce
>>> from operator import xor
>>> bits = ('0', '1', '0', '1', '0', '1', '0')
>>> reduce(xor, map(int, bits))
1

Or if you want a running XOR:

>>> from itertools import accumulate
>>> from operator import xor
>>> bits = ('0', '1', '0', '1', '0', '1', '0')
>>> list(accumulate(map(int, bits), xor))
[0, 1, 1, 0, 0, 1, 1]

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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.

    test_list = [4, 6, 2, 3, 8, 9]

    print("The original list is : " + str(test_list))

    res = reduce(lambda x, y: x ^ y, test_list)

    print("The Bitwise XOR of list elements are : " + str(res))

    Output :

    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.

    from operator import ixor

    test_list = [4, 6, 2, 3, 8, 9]

    print("The original list is : " + str(test_list))

    res = reduce(ixor, test_list)

    print("The Bitwise XOR of list elements are : " + str(res))

    Output :

    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given an array arr[] containing integers of size N, the task is to find the XOR of this array.
    Examples: 
     

    Input: arr[] = {2, 4, 7} 
    Output:
    Explanation: 
    XOR of the array = 2 ^ 4 ^ 7 = 1
    Input: arr[] = { 3, 9, 12, 13, 15 } 
    Output:
     

    Approach: In order to find the XOR of all elements in the array, we simply iterate through the array and find the XOR using ‘^’ operator. Therefore, the following steps are followed to compute the answer: 
     

    1. Create a variable to store the XOR of the array as a result.
    2. For each element in the array, find the XOR of the element and the result variable using ‘^’ operator.
    3. Finally, the result variable stores the XOR of all elements in the array.

    Below is the implementation of the above approach: 
     

    CPP

    #include

    using namespace std;

    int xorOfArray(int arr[], int n)

    {

        int xor_arr = 0;

        for (int i = 0; i < n; i++) {

            xor_arr = xor_arr ^ arr[i];

        }

        return xor_arr;

    }

    int main()

    {

        int arr[] = { 3, 9, 12, 13, 15 };

        int n = sizeof(arr) / sizeof(arr[0]);

        cout << xorOfArray(arr, n) << endl;

        return 0;

    }

    C

    #include

    int xorOfArray(int arr[], int n)

    {

        int xor_arr = 0;

        for (int i = 0; i < n; i++) {

            xor_arr = xor_arr ^ arr[i];

        }

        return xor_arr;

    }

    int main()

    {

        int arr[] = { 3, 9, 12, 13, 15 };

        int n = sizeof(arr) / sizeof(arr[0]);

        printf("%d\n", xorOfArray(arr, n));

        return 0;

    }

    Java

    class GFG {

        static int xorOfArray(int arr[], int n)

        {

            int xor_arr = 0;

            for (int i = 0; i < n; i++) {

                xor_arr = xor_arr ^ arr[i];

            }

            return xor_arr;

        }

        public static void main (String[] args)

        {

            int arr[] = { 3, 9, 12, 13, 15 };

            int n = arr.length;

            System.out.println(xorOfArray(arr, n));

        }

    }

    Python3

    def xorOfArray(arr, n):

        xor_arr = 0

        for i in range(n):

            xor_arr = xor_arr ^ arr[i]

        return xor_arr

    if __name__ == '__main__':

        arr = [3, 9, 12, 13, 15]

        n = len(arr)

        print(xorOfArray(arr, n))

    C#

    using System;

    class GFG {

        static int xorOfArray(int []arr, int n)

        {

            int xor_arr = 0;

            for (int i = 0; i < n; i++) {

                xor_arr = xor_arr ^ arr[i];

            }

            return xor_arr;

        }

        public static void Main (string[] args)

        {

            int []arr = { 3, 9, 12, 13, 15 };

            int n = arr.Length;

            Console.WriteLine(xorOfArray(arr, n));

        }

    }

    Javascript

    Time Complexity: O(N), where N is the size of the array.
     


    How do you use XOR in a list in Python?

    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.

    How do you find the XOR of consecutive elements in an array?

    Let a, b, c, d, e, f are the original elements, and the xor of every 2 consecutive elements is given, i.e a^b = k1, b ^ c = k2, c ^ d = k3, d ^ e = k4, e ^ f = k5 (where k1, k2, k3, k4, k5 are the elements that are given us along with the first element a), and we have to find the value of b, c, d, e, f.

    How do I get XOR in Python?

    In Python, we can perform the bitwise XOR operation using the "^" symbol. The XOR operation can be used for different purposes; XOR of two integers, XOR of two booleans, Swapping two numbers using XOR, etc. We can also use the xor() function using the operator module in Python.

    How do you find the XOR of 4 numbers?

    1- Initialize the result as 0. 1- Traverse all numbers from 1 to n..
    Find the remainder of n by moduling it with 4..
    If rem = 0, then XOR will be same as n..
    If rem = 1, then XOR will be 1..
    If rem = 2, then XOR will be n+1..
    If rem = 3 ,then XOR will be 0..