{"id":14016,"date":"2021-10-01T10:30:59","date_gmt":"2021-10-01T05:00:59","guid":{"rendered":"https:\/\/python-programs.com\/?p=14016"},"modified":"2021-11-22T18:33:27","modified_gmt":"2021-11-22T13:03:27","slug":"python-program-to-perform-xor-on-two-lists","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-perform-xor-on-two-lists\/","title":{"rendered":"Python Program to Perform XOR on Two Lists"},"content":{"rendered":"
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.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to perform Xor on Two lists in Python.<\/p>\n Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples<\/a><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nGiven list 1= [4, 19, 11, 5, 3, 9, 7]\r\nGiven list 2= [10, 3, 7, 2, 9, 8, 6, 5]<\/pre>\n
The given First list elements are = [4, 19, 11, 5, 3, 9, 7]\r\nThe given Second list elements are = [10, 3, 7, 2, 9, 8, 6, 5]\r\nThe result after applying xor operation on both lists is [14, 16, 12, 7, 10, 1, 1]<\/pre>\n
Given list 1 = [5, 7, 9, 6]\r\nGiven list 2 = [3, 8, 9, 4]<\/pre>\n
The given First list elements are = [5, 7, 9, 6]\r\nThe given Second list elements are = [3, 8, 9, 4]\r\nThe result after applying xor operation on both list is [6, 15, 0, 2]<\/pre>\n
Program to Perform XOR on Two Lists in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the first list as static input and store it in a variable.\r\nlstt1 = [4, 19, 11, 5, 3, 9, 7]\r\n# Give the second list as static input and store it in another variable.\r\nlstt2 = [10, 3, 7, 2, 9, 8, 6, 5]\r\nprint('The given First list elements are =', lstt1)\r\nprint('The given Second list elements are =', lstt2)\r\n# Calculate the length of the first list using the len()\r\n# function(as both lists have same length) and store it in a variable.\r\nlenlst = len(lstt1)\r\n# Loop till the above length using the For loop.\r\nfor p in range(lenlst):\r\n # Inside the for loop initialize the list 1 element as xor operation\r\n # between list1 and list2 using xor operator i.e lst1[p]=lst1[p]^lst2[p]\r\n # where p is the iterator value of the For loop.\r\n lstt1[p] = lstt1[p] ^ lstt2[p]\r\n# Print the list1 which is the result.\r\nprint('The result after applying xor operation on both lists is', lstt1)\r\n<\/pre>\n
The given First list elements are = [4, 19, 11, 5, 3, 9, 7]\r\nThe given Second list elements are = [10, 3, 7, 2, 9, 8, 6, 5]\r\nThe result after applying xor operation on both lists is [14, 16, 12, 7, 10, 1, 1]<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the first list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\nlstt1 = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the second list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\nlstt2 = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\nprint('The given First list elements are =', lstt1)\r\nprint('The given Second list elements are =', lstt2)\r\n# Calculate the length of the first list using the len()\r\n# function(as both lists have same length) and store it in a variable.\r\nlenlst = len(lstt1)\r\n# Loop till the above length using the For loop.\r\nfor p in range(lenlst):\r\n # Inside the for loop initialize the list 1 element as xor operation\r\n # between list1 and list2 using xor operator i.e lst1[p]=lst1[p]^lst2[p]\r\n # where p is the iterator value of the For loop.\r\n lstt1[p] = lstt1[p] ^ lstt2[p]\r\n# Print the list1 which is the result.\r\nprint('The result after applying xor operation on both lists is', lstt1)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 5 7 9 6\r\nEnter some random List Elements separated by spaces = 3 8 9 4\r\nThe given First list elements are = [5, 7, 9, 6]\r\nThe given Second list elements are = [3, 8, 9, 4]\r\nThe result after applying xor operation on both list is [6, 15, 0, 2]<\/pre>\n
\n