Python difference between two lists of strings

List preserves the order of the elements in the list. When you compare the two lists using == operator, it returns True if all the elements in the lists are the same and in the same order.

list_a = [2, 4, 6, 8] list_b = [2, 4, 6, 8] print[list_a==list_b]

Output:

True

If you change the order of the elements in any of the list and then compare it with other, it returns False.

list_a = [2, 4, 6, 8] list_b = [2, 8, 6, 4] print[list_a==list_b]

Output:

False

Now we want to compare the two list irrespective of their order.

Compare Two Lists in Python

Table of Contents

  • 1. Using Membership Operator
  • 2. Using Set Method
  • 3. Using Sort Method
  • 4. Return Non-Matches Elements with For Loop
  • 5. Difference Between Two List
  • 6. Lambda Function To Return All Unmatched Elements

1. Using Membership Operator

We can compare the list by checking if each element in the one list present in another list.

def is_identical[list_a, list_b]: if len[list_a] != len[list_b]: return False for i in list_a: if i not in list_b: return False return True list_a = [2, 4, 6, 8] list_b = [2, 8, 6, 4] if is_identical[list_a, list_b]: print["Two lists are identical."] else: print["Two lists are different."]

Output:

Two lists are identical.

You can read more about Python list and its various method.

This is a tedious job as we have to traverse through the complete list.

You have to take one element from the list A and needs to search if the element present in list B.

As the list is not sorted by default, for each element, it will take O[n] time to search the element in another list.

So, the total complexity will be O[n^2].

This is good for learning and definitely not the standard way of writing the Python code.

Can we optimize this problem?

Basically we need to find out the solution to solve this problem without traversing the complete list.

There is way of doing this. We can use the Python inbuilt functions for comparing two lists.

Lets see.

2. Using Set Method

To compare two lists, we are using the set method.

  • If the length of the two lists is different, the list can not be identical and return False.
  • Else, Convert both the lists into sets.
  • Compare these two sets. If the sets are equal, two given lists are the same. Otherwise, two lists are different.
def is_identical[list_a, list_b]: if len[list_a] != len[list_b]: return False if set[list_a] == set[list_b]: return True else: return False list_a = [2, 4, 6, 8] list_b = [2, 8, 6, 4] if is_identical[list_a, list_b]: print["Two lists are identical."] else: print["Two lists are different."]

Output:

Two lists are identical.

Drawback of using set[] method:

The set[] method removes all the duplicate elements in the list. If your list contains duplicate elements, this solution will not work for you.

Lets see how you can take a rid of this problem to compare two lists in Python.

3. Using Sort Method

We can sort the two given list and then compare.

We have already learned about two ways of sorting the Python list in the previous tutorial. Lets use that to find if the two lists are the same.

list_a = [2, 4, 6, 6] list_b = [2, 6, 4, 6] def is_identical[list_a, list_b]: list_a.sort[] list_b.sort[] return list_a == list_b if is_identical[list_a, list_b]: print["Two lists are identical."] else: print["Two lists are different."]

Output:

Two lists are identical.

This solution will work even if the lists have duplicate elements.

Now, we are increasing the scope of our problem. Instead of checking if the two lists are the same or not, we also want to find the non-match elements in the list.

4. Return Non-Matches Elements with For Loop

You can write a function to return all unmatched elements in the list using for loop.

def non_match_elements[list_a, list_b]: non_match = [] for i in list_a: if i not in list_b: non_match.append[i] return non_match list_a = [2, 4, 6, 8, 10, 12] list_b = [2, 4, 6, 8] non_match = non_match_elements[list_a, list_b] print["No match elements: ", non_match]

Output:

No match elements: [10, 12]

Instead of just for loop, you can use other looping mechanisms in Python.

You can also modify the above code to return the index of non-match elements in the list.

5. Difference Between Two List

We can perform the subtraction operation on set to find the extra elements in one list from another list.

def get_difference[list_a, list_b]: return set[list_a]-set[list_b] list_a = [2, 4, 6, 8, 10, 12] list_b = [2, 4, 6, 8] non_match = list[get_difference[list_a, list_b]] print["No match elements: ", non_match]

Output:

No match elements: [10, 12]

Now, lets find the unmatched elements from both the list.

def get_difference[list_a, list_b]: non_match_a = set[list_a]-set[list_b] non_match_b = set[list_b]-set[list_a] non_match = list[non_match_a] + list[non_match_b] return non_match list_a = [2, 4, 6, 8, 10, 12] list_b = [2, 4, 6, 8, 9, 13] non_match = get_difference[list_b, list_a] print["Non-match elements: ", non_match]

Output:

Non-match elements: [9, 13, 10, 12]

This will give you all the unmatched elements from both the lists.

Lets make it more interesting with some of the Python advanced concepts.

6. Lambda Function To Return All Unmatched Elements

We can rewrite the above regular function using the lambda function to get all the non-matched elements in the lists.

l_func = lambda x, y: list[[set[x]- set[y]]] + list[[set[y]- set[x]]] list_a = [2, 4, 6, 8, 10, 12] list_b = [2, 4, 6, 8, 9, 13] non_match = l_func[list_a, list_b] print["Non-match elements: ", non_match]

Output:

Non-match elements: [10, 12, 9, 13]

Python is interesting and you can find many ways of comparing two lists.

Use-cases

I was working on one of the projects where Im calling REST API. After getting a web response form REST API, I have to validate all the HTTP header fields whether all the required attributes are present or not.

The easiest way is to keep the list of all the expected attributes and then compare it with the list of attributes getting from the REST API response. If the two lists are not the same, through an error telling attribute missing.

Similarly, you can find the many other use-cases where you need to compare two or more Python list to find unmatched elements..

Other List Related Tutorials for Practice:

  • Check if all the Elements in the List are Same
  • Find sum of all elements in the list
  • Sorting List of Tuples in Python

In this tutorial, we have learned different methods to compare two lists in Python and return non match elements. If you have any doubts or if you know any other way of comparing two lists, share it with me in the comment.

Happy Pythoning!

Pythonpython list

Video liên quan

Chủ Đề