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
test_list1 = [ "a" , "b" , "c" , "d" ]
test_list2 = [ "g" , "b" , "s" , "d" ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = []
for i, j in zip (test_list1, test_list2):
if i ! = j:
res.append(i)
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
test_list1 = [ "a" , "b" , "c" , "d" ]
test_list2 = [ "g" , "b" , "s" , "d" ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = [i for i, j in zip (test_list1, test_list2) if i ! = j]
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
test_list1 = [ "a" , "b" , "c" , "d" ]
test_list2 = [ "g" , "b" , "s" , "d" ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = [test_list1[i]
for i, val in enumerate (test_list1) if val ! = test_list2[i]]
print ( "Unequal index elements in lists : " + str (res))
|
OutputThe 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
test_list1 = [ "a" , "b" , "c" , "d" ]
test_list2 = [ "g" , "b" , "s" , "d" ]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = list ( filter ( lambda x: x[ 0 ] ! = x[ 1 ], zip (test_list1, test_list2)))
print ( "Unequal index elements in lists : " + str ([r[ 0 ] for r in res]))
|
OutputThe 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
test_list1 = [ "a" , "b" , "c" , "d" ]
test_list2 = [ "g" , "b" , "s" , "d" ]
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)
res = arr1[arr1 ! = arr2]
result_list = res.tolist()
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)