Monday, October 6, 2025
HomeLanguagesPython | Interlist XOR

Python | Interlist XOR

There can be many situations in which one requires to find index wise XOR of two different lists. This can have a possible applications in day-day programming. Lets discuss various ways in which this task can be performed. 

Method #1 : Naive Method In this method, we simply run a loop and append to the new list the XOR of the both list elements at similar index till we reach end of the smaller list. This is the basic method to achieve this task. 

Python3




# Python code to demonstrate
# Interlist XOR
# naive method
 
# initializing lists
test_list1 = [1, 3, 4, 6, 8]
test_list2 = [4, 5, 6, 2, 10]
 
# printing original lists
print ("Original list 1 : " + str(test_list1))
print ("Original list 2 : " + str(test_list2))
 
# using naive method to
# Interlist XOR
res_list = []
for i in range(0, len(test_list1)):
    res_list.append(test_list1[i] ^ test_list2[i])
 
# printing resultant list
print ("Resultant list is : " + str(res_list))


Output : 

Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 6, 2, 4, 2]

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

  Method #2 : Using List Comprehension The shorthand for the above explained technique, list comprehensions are usually quicker to type and hence must be preferred to perform these kind of programming tasks. 

Python3




# Python code to demonstrate
# Interlist XOR
# list comprehension
 
# initializing lists
test_list1 = [1, 3, 4, 6, 8]
test_list2 = [4, 5, 6, 2, 10]
 
# printing original lists
print ("Original list 1 : " + str(test_list1))
print ("Original list 2 : " + str(test_list2))
 
# using list comprehension to
# Interlist XOR
res_list = [test_list1[i] ^ test_list2[i] for i in range(len(test_list1))]
 
# printing resultant list
print ("Resultant list is : " + str(res_list))


Output : 

Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 6, 2, 4, 2]

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

Method #4 : Using itertools.starmap()
The itertools.starmap() can be used to make this task much more compact and elegant. This method also performs the same task as above methods.
 

Python3




# Python code to demonstrate
# Interlist XOR
# using itertools.starmap()
   
# initializing lists
test_list1 = [1, 3, 4, 6, 8]
test_list2 = [4, 5, 6, 2, 10]
   
# printing original lists
print ("Original list 1 : " + str(test_list1))
print ("Original list 2 : " + str(test_list2))
   
# using itertools.starmap() to
# Interlist XOR
import itertools
res_list = list(itertools.starmap(lambda i, j : i ^ j, zip(test_list1, test_list2)))
   
# printing resultant list
print ("Resultant list is : " + str(res_list))
#This code is contributed by Edula Vinay Kumar Reddy


Output

Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 6, 2, 4, 2]

Time Complexity: O(n) where n is the length of the shorter list.
Space Complexity: O(n) where n is the length of the shorter list.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6707 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6825 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6781 POSTS0 COMMENTS