How do you xor a list in python?

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 solve 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.

    What is XOR command?

    XOR is a bitwise operator, and it stands for "exclusive or." It performs logical operation. If input bits are the same, then the output will be false(0) else true(1).