Open In App

Python – Filter unequal elements of two lists corresponding same index

Last Updated : 09 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python data, we can have a problem in which we require to extract the values across multiple lists which are unequal and have similar index. This kind of problem can come in many domains. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using loop + zip()

The combination of the above functions can be used to solve this problem. In this, we extract and combine the index elements using zip and then extract and check for dissimilarity using conditional statement in loop.

Python3




# Python3 code to demonstrate working of
# Unequal Equi-index elements
# using loop + zip()
 
# Initialize lists
test_list1 = ["a", "b", "c", "d"]
test_list2 = ["g", "b", "s", "d"]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Unequal Equi-index elements
# using loop + zip()
res = []
for i, j in zip(test_list1, test_list2):
    if i != j:
        res.append(i)
 
# printing result
print("Unequal index elements in lists : " + str(res))


Output : 

The original list 1 : ['a', 'b', 'c', 'd']
The original list 2 : ['g', 'b', 's', 'd']
Unequal index elements in lists : ['a', 'c']

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 zip() + list comprehension Combination of these functionalities can also be used to solve this problem. In this, we use a similar method as above, just a shorthand logic compressed using list comprehension. 

Python3




# Python3 code to demonstrate working of
# Unequal Equi-index elements
# using list comprehension + zip()
 
# initialize lists
test_list1 = ["a", "b", "c", "d"]
test_list2 = ["g", "b", "s", "d"]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Unequal Equi-index elements
# using list comprehension + zip()
res = [i for i, j in zip(test_list1, test_list2) if i != j]
 
# printing result
print("Unequal index elements in lists : " + str(res))


Output : 

The original list 1 : ['a', 'b', 'c', 'd']
The original list 2 : ['g', 'b', 's', 'd']
Unequal index elements in lists : ['a', 'c']

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 #3: Using list comprehension + enumerate()

This method utilizes the enumerate() function to iterate through the indices of test_list1 and compares the elements of test_list1 and test_list2 at the same index using list comprehension.

Python3




# Python3 code to demonstrate working of
# Unequal Equi-index elements
# using list comprehension + enumerate()
 
# initialize lists
test_list1 = ["a", "b", "c", "d"]
test_list2 = ["g", "b", "s", "d"]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Unequal Equi-index elements
# using list comprehension + enumerate()
res = [test_list1[i]
       for i, val in enumerate(test_list1) if val != test_list2[i]]
 
# printing result
print("Unequal index elements in lists : " + str(res))


Output

The original list 1 : ['a', 'b', 'c', 'd']
The original list 2 : ['g', 'b', 's', 'd']
Unequal index elements in lists : ['a', 'c']

Time complexity: O(n), where n is the length of the lists.
Auxiliary space: O(k), where k is the number of unequal elements.

Method 4: using filter() + lambda

Python3




# Python3 code to demonstrate working of Unequal Equi-index elements
# using filter() + lambda
 
# Initialize lists
test_list1 = ["a", "b", "c", "d"]
test_list2 = ["g", "b", "s", "d"]
 
# Printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Unequal Equi-index elements
# using filter() + lambda
res = list(filter(lambda x: x[0] != x[1], zip(test_list1, test_list2)))
 
# Printing result
print("Unequal index elements in lists : " + str([r[0] for r in res]))


Output

The original list 1 : ['a', 'b', 'c', 'd']
The original list 2 : ['g', 'b', 's', 'd']
Unequal index elements in lists : ['a', 'c']

Time complexity: O(n), where n is the length of the input lists. 
Auxiliary space: O(n) because of the res list created to store the unequal elements.

Method 5 : Using numpy array subtraction

Python3




import numpy as np
 
# Initialize lists
test_list1 = ["a", "b", "c", "d"]
test_list2 = ["g", "b", "s", "d"]
 
# Convert lists to numpy arrays
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)
 
# Use numpy array subtraction to find unequal elements
res = arr1[arr1 != arr2]
 
# Convert result back to list
result_list = res.tolist()
 
# Print the result
print("Unequal index elements in lists : " + str(result_list))


OUTPUT : 
Unequal index elements in lists : ['a', 'c']

Time complexity: O(n)
Auxiliary space: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads