Program to Perform XOR on Two Lists

Python Program to Perform XOR on Two Lists

Given two lists of the same length, the task is to perform the Xor Operation on both the list elements which are having the same index in Python.

Examples:

Example1:

Input:

Given list 1= [4, 19, 11, 5, 3, 9, 7]
Given list 2= [10, 3, 7, 2, 9, 8, 6, 5]

Output:

The given First list elements are = [4, 19, 11, 5, 3, 9, 7]
The given Second list elements are = [10, 3, 7, 2, 9, 8, 6, 5]
The result after applying xor operation on both lists is [14, 16, 12, 7, 10, 1, 1]

Example2:

Input:

Given list 1 = [5, 7, 9, 6]
Given list 2 = [3, 8, 9, 4]

Output:

The given First list elements are = [5, 7, 9, 6]
The given Second list elements are = [3, 8, 9, 4]
The result after applying xor operation on both list is [6, 15, 0, 2]

Program to Perform XOR on Two Lists in Python

Below are the ways to perform Xor on Two lists in Python.

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Method #1: Using For Loop (Static Input)

Approach:

  • Give the first list as static input and store it in a variable.
  • Give the second list as static input and store it in another variable.
  • Calculate the length of the first list using the len() function(as both lists have same length) and store it in a variable.
  • Loop till the above length using the For loop.
  • Inside the for loop initialize the list 1 element as xor operation between list1 and list2 using xor operator i.e lst1[p]=lst1[p]^lst2[p] where p is the iterator value of the For loop.
  • Print the list1 which is the result.
  • The Exit of Program.

Below is the implementation:

# Give the first list as static input and store it in a variable.
lstt1 = [4, 19, 11, 5, 3, 9, 7]
# Give the second list as static input and store it in another variable.
lstt2 = [10, 3, 7, 2, 9, 8, 6, 5]
print('The given First list elements are =', lstt1)
print('The given Second list elements are =', lstt2)
# Calculate the length of the first list using the len()
# function(as both lists have same length) and store it in a variable.
lenlst = len(lstt1)
# Loop till the above length using the For loop.
for p in range(lenlst):
    # Inside the for loop initialize the list 1 element as xor operation
    # between list1 and list2 using xor operator i.e lst1[p]=lst1[p]^lst2[p]
    # where p is the iterator value of the For loop.
    lstt1[p] = lstt1[p] ^ lstt2[p]
# Print the list1 which is the result.
print('The result after applying xor operation on both lists is', lstt1)

Output:

The given First list elements are = [4, 19, 11, 5, 3, 9, 7]
The given Second list elements are = [10, 3, 7, 2, 9, 8, 6, 5]
The result after applying xor operation on both lists is [14, 16, 12, 7, 10, 1, 1]

Method #2: Using For Loop (User Input)

Approach:

  • Give the first list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the first list using the len() function(as both lists have same length) and store it in a variable.
  • Loop till the above length using the For loop.
  • Inside the for loop initialize the list 1 element as xor operation between list1 and list2 using xor operator i.e lst1[p]=lst1[p]^lst2[p] where p is the iterator value of the For loop.
  • Print the list1 which is the result.
  • The Exit of Program.

Below is the implementation:

# Give the first list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
lstt1 = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the second list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
lstt2 = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
print('The given First list elements are =', lstt1)
print('The given Second list elements are =', lstt2)
# Calculate the length of the first list using the len()
# function(as both lists have same length) and store it in a variable.
lenlst = len(lstt1)
# Loop till the above length using the For loop.
for p in range(lenlst):
    # Inside the for loop initialize the list 1 element as xor operation
    # between list1 and list2 using xor operator i.e lst1[p]=lst1[p]^lst2[p]
    # where p is the iterator value of the For loop.
    lstt1[p] = lstt1[p] ^ lstt2[p]
# Print the list1 which is the result.
print('The result after applying xor operation on both lists is', lstt1)

Output:

Enter some random List Elements separated by spaces = 5 7 9 6
Enter some random List Elements separated by spaces = 3 8 9 4
The given First list elements are = [5, 7, 9, 6]
The given Second list elements are = [3, 8, 9, 4]
The result after applying xor operation on both list is [6, 15, 0, 2]

Related Programs: