Find non matching elements in two lists python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Checking a number/element by a condition is a common problem one faces and is done in almost every program. Sometimes we also require to get the totals number that does not match the particular condition to have a distinguish which match for further utilization. Lets discuss certain ways in which this task can be achieved.

    Method #1 : Using loop 
    This is brute force method to perform this particular task. In this, we iterate list, find elements that does not match a particular condition and take count. 

    Python3

    test_list = [3, 5, 1, 6, 7, 9]

    print ("The original list is : " + str(test_list))

    res = 0

    for ele in test_list:

        if not ele % 2 != 0:

            res = res + 1

    print ("The number of non-odd elements: " + str(res))

    Output : 

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of non-odd elements: 1

    Method #2 : Using len() + generator expression 
    This method uses the trick of counting elements to the add 1 whenever the generator expression returns False. By the time list gets exhausted, count of numbers not matching a condition is returned.

    Python3

    test_list = [3, 5, 1, 6, 7, 9]

    print ("The original list is : " + str(test_list))

    res = len(list(i for i in test_list if not i % 2 != 0))

    print ("The number of non-odd elements: " + str(res))

    Output : 

    The original list is : [3, 5, 1, 6, 7, 9]
    The number of non-odd elements: 1


    There are various ways in which the difference between two lists can be generated. In this article, we will see the different ways to Get the difference between two lists which can be done using Python.

    Examples:  

    Input:
    list1 = [10, 15, 20, 25, 30, 35, 40]
    list2 = [25, 40, 35] 
    
    Output:
    [10, 20, 30, 15]
    
    Explanation:
    resultant list = list1 - list2

     Note: When you have multiple same elements then this would not work. In that case, this code will simply remove the same elements.
    In that case, you can maintain a count of each element in both lists.

    Method 1: Use “in” to Find the Difference Between Two Lists in Python

    In this example, we are using loop and Python in keyword to find the difference between two lists in Python.

    Python3

    li1 = [10, 15, 20, 25, 30, 35, 40]

    li2 = [25, 40, 35]

    temp3 = []

    for element in li1:

        if element not in li2:

            temp3.append(element)

    print(temp3)

    Output:

    [10, 15, 20, 30]

    Method 2: Use set() to Find the Difference Between Two Lists in Python

    Python3

    li1 = [10, 15, 20, 25, 30, 35, 40]

    li2 = [25, 40, 35]

    s = set(li2)

    temp3 = [x for x in li1 if x not in s]

    print(temp3)

    Output :  

    [10, 15, 20, 30]

    Method 3:  Use a list comprehension and set to Find the Difference Between Two Lists in Python

    In this method, we convert the lists into sets explicitly and then simply reduce one from the other using the subtract operator. For more references on set visit Sets in Python. It is a similar technique that we used previously. The only difference is, that we replaced the nested loops with the list comprehension syntax.

    Python3

    li1 = [10, 15, 20, 25, 30, 35, 40]

    li2 = [25, 40, 35]

    s = set(li2)

    temp3 = [x for x in li1 if x not in s]

    print(temp3)

    Output:

    [10, 15, 20, 30]

    Method 4: Without using the set()

    In this method, we use the basic combination technique to copy elements from both lists with a regular check if one is present in the other or not. 

    Python3

    def Diff(li1, li2):

        li_dif = [i for i in li1 + li2 if i not in li1 or i not in li2]

        return li_dif

    li1 = [10, 15, 20, 25, 30, 35, 40]

    li2 = [25, 40, 35]

    li3 = Diff(li1, li2)

    print(li3)

    Output : 

    [10, 15, 20, 30]

    Method 5: Use Numpy to Find the Difference Between Two Lists in Python

    The numpy.concatenate() function concatenate a sequence of arrays along an existing axis.

    Python3

    import numpy as np

    li1 = np.array([10, 15, 20, 25, 30, 35, 40])

    li2 = np.array([25, 40, 35])

    dif1 = np.setdiff1d(li1, li2)

    dif2 = np.setdiff1d(li2, li1)

    temp3 = np.concatenate((dif1, dif2))

    print(list(temp3))

    Output:

    [10, 15, 20, 30]

    Method 6: Use symmetric_difference to Find the Difference Between Two Lists in Python

    The elements that are either in the first set or the second set are returned using the symmetric_difference() technique. The intersection, unlike the shared items of the two sets, is not returned by this technique.

    Python3

    li1 = [10, 15, 20, 25, 30, 35, 40]

    li2 = [25, 40, 35]

    set_dif = set(li1).symmetric_difference(set(li2))

    temp3 = list(set_dif)

    print(temp3)

    Output:

    [20, 10, 30, 15]

    How do you find the uncommon element in two lists in Python?

    The most efficient and recommended method to perform this task is using the combination of set() and map() to achieve it. Firstly converting inner lists to tuples using map, and outer lists to set, use of ^ operator can perform the set symmetric difference and hence perform this task.

    How do you compare two elements in Python and return Non matches?

    Using Membership Operator. We can compare the list by checking if each element in the one list present in another list. ... .
    Using Set Method. ... .
    Using Sort Method. ... .
    Return Non-Matches Elements with For Loop. ... .
    Difference Between Two List. ... .
    Lambda Function To Return All Unmatched Elements..

    How do you compare elements of two different lists?

    The methods of comparing two lists are given below..
    The cmp() function..
    The set() function and == operator..
    The sort() function and == operator..
    The collection.counter() function..
    The reduce() and map() function..

    How do you compare two lists of items in Python?

    Using list. sort() method sorts the two lists and the == operator compares the two lists item by item which means they have equal data items at equal positions. This checks if the list contains equal data item values but it does not take into account the order of elements in the list.