Open In App

Python – Elements with K lists similar index value

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# Elements with K lists similar index value
# using zip() + loop
 
# Initializing lists
test_list1 = [1, 3, 5, 7]
test_list2 = [1, 4, 8, 9]
test_list3 = [3, 7, 5, 10]
 
# printing original lists
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))
 
# Initializing K
K = 2
 
# Elements with K lists similar index value
# using zip() + loop
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)
 
# printing result
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




# Python3 code to demonstrate
# Elements with K lists similar index value
# using list comprehension + count() + set()
 
# Initializing lists
test_list1 = [1, 3, 5, 7]
test_list2 = [1, 4, 8, 9]
test_list3 = [3, 7, 5, 10]
 
# printing original lists
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))
 
# Initializing K
K = 2
 
# Elements with K lists similar index value
# using list comprehension + count() + set()
res = [ele for sub in zip(test_list1, test_list2, test_list3) for ele in set(sub) if sub.count(ele) > 1]
 
# printing result
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




# Python3 code to demonstrate
# Elements with K lists similar index value
# using map() + lambda function
 
# Initializing lists
test_list1 = [1, 3, 5, 7]
test_list2 = [1, 4, 8, 9]
test_list3 = [3, 7, 5, 10]
 
# printing original lists
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))
 
# Initializing K
K = 2
 
# Elements with K lists similar index value
# using map() + lambda function
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)))
 
# printing result
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 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




# Initializing lists
test_list1 = [1, 3, 5, 7]
test_list2 = [1, 4, 8, 9]
test_list3 = [3, 7, 5, 10]
 
# Initializing K
K = 2
 
# Elements with K lists similar index value using list comprehension and enumerate()
res = [val for i, val in enumerate(test_list1) if val in test_list2[i:]+test_list3[i:]]
        
# printing result
print("The list after checking on 2 lists: " + str(res))


Output

The 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)



Last Updated : 13 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads