Python – Elements with K lists similar index value

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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]

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.