Sometimes, while working with data, we can have a problem in which we need to get elements which are similar in K lists in particular index. This can have application in many domains such as day-day and other domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using zip() + loop This is brute way in which this task can be performed. In this, we iterate the loop in zipped list and compare with elements in particular index for similarity.
Python3
test_list1 = [ 1 , 3 , 5 , 7 ]
test_list2 = [ 1 , 4 , 8 , 9 ]
test_list3 = [ 3 , 7 , 5 , 10 ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
print ("The original list 3 is : " + str (test_list3))
K = 2
res = []
for a, b, c in zip (test_list1, test_list2, test_list3):
if a = = b or b = = c or c = = a:
if a = = b:
res.append(a)
elif b = = c:
res.append(b)
elif c = = a:
res.append(c)
print ("The list after checking on 2 lists : " + str (res))
|
Output : The original list 1 is : [1, 3, 5, 7]
The original list 2 is : [1, 4, 8, 9]
The original list 3 is : [3, 7, 5, 10]
The list after checking on 2 lists : [1, 5]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using list comprehension + set() + count() The combination of above methods can also be used to perform this task. In this, we check for count using count() and rest of functionalities are performed using list comprehension.
Python3
test_list1 = [ 1 , 3 , 5 , 7 ]
test_list2 = [ 1 , 4 , 8 , 9 ]
test_list3 = [ 3 , 7 , 5 , 10 ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
print ("The original list 3 is : " + str (test_list3))
K = 2
res = [ele for sub in zip (test_list1, test_list2, test_list3) for ele in set (sub) if sub.count(ele) > 1 ]
print ("The list after checking on 2 lists : " + str (res))
|
Output : The original list 1 is : [1, 3, 5, 7]
The original list 2 is : [1, 4, 8, 9]
The original list 3 is : [3, 7, 5, 10]
The list after checking on 2 lists : [1, 5]
Time complexity: O(n^2) (due to the use of zip and count())
Auxiliary space: O(n)
Method #3: Using map() + lambda function
In this method, use the map() function along with a lambda function to iterate over the three input lists and check if any two elements are equal at the same index. Filter the resulting list to remove any None values and return the final list of matching elements.
Python3
test_list1 = [ 1 , 3 , 5 , 7 ]
test_list2 = [ 1 , 4 , 8 , 9 ]
test_list3 = [ 3 , 7 , 5 , 10 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
print ( "The original list 3 is : " + str (test_list3))
K = 2
res = list ( filter ( lambda x: x ! = None , map ( lambda x, y, z: x if x = = y or y = = z or z = = x else None , test_list1, test_list2, test_list3)))
print ( "The list after checking on 2 lists : " + str (res))
|
OutputThe original list 1 is : [1, 3, 5, 7]
The original list 2 is : [1, 4, 8, 9]
The original list 3 is : [3, 7, 5, 10]
The list after checking on 2 lists : [1, 5]
Time Complexity: O(n), where n is the length of the input lists since we need to iterate over all the elements of the input lists once.
Auxiliary Space: O(n), where n is the length of the input lists since we create a list of the same size as the input lists to store the result.
Method #4: Using list comprehension and enumerate() method:
Step-by-step approach:
- Initialize the three lists test_list1, test_list2, and test_list3.
- Initialize the value of K as 2.
- Use list comprehension and the enumerate() method to iterate through the first list, test_list1.
- Check if the current value of test_list1 is present in either test_list2 or test_list3, by slicing those two lists starting from the current index using the expression test_list2[i:]+test_list3[i:]. If the value is present in either list, we append it to the result list, res.
- print the final result.
Python3
test_list1 = [ 1 , 3 , 5 , 7 ]
test_list2 = [ 1 , 4 , 8 , 9 ]
test_list3 = [ 3 , 7 , 5 , 10 ]
K = 2
res = [val for i, val in enumerate (test_list1) if val in test_list2[i:] + test_list3[i:]]
print ( "The list after checking on 2 lists: " + str (res))
|
OutputThe list after checking on 2 lists: [1, 5]
Time complexity: O(n^2) (worst case, when all lists have same length and K is equal to the length of the lists)
Auxiliary space: O(1) (constant space for variables)