Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Filter unequal elements of two lists corresponding same index

  • Last Updated : 27 Dec, 2019

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 above functions can be used to solve this problem. In this, we extract combine the index elements using zip and then extract and check for dissimilarity using conditional statement in loop.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




# 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']

 

Method #2 : Using zip() + list comprehension
Combination of these functionalities can also be used to solve this problem. In this, we use similar method as above, just a shorthand logic compressed using list comprehension.




# 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']



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!