Python diff between lists

Python to Find Difference Between Two Lists

In this tutorial, we’ll discover two Pythonic ways to find the Difference Between Two Lists. One of the methods is using the Python Set. It first converts the lists into sets and then gets the unique part out of that.

Other non-set methods compare two lists element by element and collect the unique ones. We can implement these by using nested for loops and with the list comprehension.

By the way, if you are not aware of the sets in Python, then follow the below tutorial. It would quickly introduce you to how Python implements the mathematical form of Set.

Pythonic Ways to Find the Difference Between Two Lists

Python Set seems to be the most obvious choice to identify the common as well as the difference of two lists. So, we are going to explore it first and then will use nested loops and list comprehension.

# Test Input list_a = [11, 16, 21, 26, 31, 36, 41] list_b = [26, 41, 36]

And we want our solution to provide the following output:

# Expected Result # list_out = list_a - list_b list_out = [11, 21, 31, 16]

Let’s start to create a program to find the difference between two lists, first using sets.

Читайте также:  JavaHelp

In this approach, we’ll first derive two SETs (say set1 and set2) from the LISTs (say list1 and list2) by passing them to set() function. After that, we’ll perform the set difference operation. It will return those elements from list1 which don’t exist in the second.

Here is the sample Python program to get the difference of two lists.

""" Desc: Using set() to find the difference between two lists in Python """ def list_diff(list1, list2): return (list(set(list1) - set(list2))) # Test Input list1 = [11, 16, 21, 26, 31, 36, 41] list2 = [26, 41, 36] # Run Test print(list_diff(list1, list2))

After running this, you should see the following outcome:

In this method, we’ll use nested For Loop to compare each element of the first list with the second. And while traversing, we’ll be appending every non-matching item to a new (and empty) list.

The new list would finally include the difference between the two given sequences. Below is the sample program to demonstrate this approach.

""" Desc: Nested loop to find the difference between two lists in Python """ def list_diff(list1, list2): out = [] for ele in list1: if not ele in list2: out.append(ele) return out # Test Input list1 = [11, 16, 21, 26, 31, 36, 41] list2 = [26, 41, 36] # Run Test print(list_diff(list1, list2))

After running the above program, you should see the following outcome:

It is almost a similar technique that we used in the previous one. Here, we replaced the nested loops with the list comprehension syntax.

Also, to learn Python from scratch to depth, do read our step by step Python tutorial .

Источник

Python How to Find the Difference Between Two Lists

To find the asymmetric difference between two lists in Python:

  1. Convert the lists to sets.
  2. Subtract the sets from one another to find the difference.
  3. Convert the result back to a list.
names1 = ["Alice", "Bob", "Charlie", "David"] names2 = ["Bob", "Charlie"] difference = list(set(names1) - set(names2)) print(difference)

Asymmetric Difference between Lists in Python

The asymmetric difference in set theory means that given sets A and B, return the elements that are in set A but not in set B. But do not return the elements that are in set B but not in A.

This image illustrates the idea:

The example you saw above is about the asymmetric difference between two lists in Python.

names1 = ["Alice", "Bob"] names2 = ["Bob", "Charlie"] difference = list(set(names1) - set(names2)) print(difference)

Even though you might expect [«Alice», «Charlie»] as neither name occurs in both sets. But because the asymmetric difference is about finding only the elements that are in set A but not in B, it only returns “Alice”.

To fix this problem, you need to calculate the symmetric difference between the two lists.

Symmetric Difference between Lists in Python

The symmetric difference in set theory means given sets A and B, return the elements that are in set A but not in set B and vice versa.

To get the symmetric difference between two lists in Python:

  1. Convert the lists to sets.
  2. Use the built-in symmetric_difference() function of sets to get the symmetric difference.
  3. Convert the result back to a list.
names1 = ["Alice", "Bob"] names2 = ["Bob", "Charlie"] difference = list(set(names1).symmetric_difference(set(names2))) print(difference)

Now both names that do not occur in both lists are in the result.

Conclusion

The Asymmetric difference between two lists in Python returns the items that list A has but list B does not. This means if list B has an item that is not found in A, it does not count.

To calculate the asymmetric difference in Python lists:

  1. Convert the lists to sets.
  2. Compute the difference by subtracting one set from the other.
  3. Convert the result to a list.

The symmetric difference between two lists in Python returns the items in list A that are not in list B and vice versa. To calculate the symmetric difference in Python lists:

  1. Convert the lists to sets.
  2. Compute the difference with symmetric_difference() method.
  3. Convert the result to a list.

Thanks for reading. Happy coding!

Further Reading

Источник

Python List Difference: Find the Difference between 2 Python Lists

Python List Difference Cover Image

In this post, you’ll learn how to find the Python list difference, meaning what items are different between two lists.

In particular, you’ll learn: how to find the difference between lists when order and duplicates matters, when they don’t matter, and how to find the symmetrical difference between two lists.

The Short Answer: Use Set Subtraction

list1 = [1,2,3,4,5,6] list2 = [2,4,5,7] difference = list(set(list1) - set(list2)) print(difference) # Returns [1, 3, 6]

What is Python List Difference?

Python list difference refers to finding the items that exist in one list, but not in the other. There are two main things to understand here:

  1. The comparison ordering matters: if you want to find the items in one list but not in the other, you need to use that are your comparator
  2. Whether repetition matters: since lists can contain duplicate values, some methods will work better for this

Comparison Ordering

Say we have two different lists:

list1 = [1,2,3,4,5,6] list2 = [2,4,5,7] 

If we were to calculate the difference between list1 and list2, we would find that list1 contains [1, 3, 6] , while list2 doesn’t.

If we were to calculate the difference between list2 and list1, we would find that list2 contains [7] , while list1 doesn’t.

If you’re looking to find the list of items that exist in either list but not both, check out the section on finding Python symmetrical difference.

Duplicates in Lists

Now, to understand better, let’s look at three different lists:

list1 = [1,2,3,4,5,6] list1b = [1,2,3,3,4,5,6] list2 = [2,4,5,7]

We have lists list1 and list2 which contain only unique items. However, we also have list1b that contains two 3’s. Were we to calculate a duplicate list difference:

  • Between list1 and list2 , we would return [1,3,6] .
  • However, were we do to the same between list1b and list2 , we would return [1,3,3,6] .

Now, were we to calculate non-repetitve list differences, both differences would simply be [1,3,6] .

Calculate List Difference with Duplicate Items with a For Loop

In order to calculate a repetitive list difference in Python, we can use a for-loop. For a refresher on for-loops, check out my post here and my video tutorial here:

Источник

Difference between Two Lists in Python

Python Certification Course: Master the essentials

The difference between the two lists in Python (first list — second list ) results in items that are in the first list but not in the second list.

Similarly, the difference between the second list and the first list (second list — first list) results in the items that are in the second list but not in the first list.

Symmetric difference between the list gives the items which are either in the first list or the second list but not in both.

For example

First list = [1,2,9] second list = [8,7,9]

  • difference between first list and second list = [1,2]
  • difference between second list and first list = [8,7]
  • symmetric difference between the lists = [1,2,8,7]

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

In this method to find the difference between two lists in Python, iterate over the first list using for loop, and for each item in the first list check if the item is present in the second list using the «not in» syntax of Python. If the item is not found in the second list then append the item to a new list.

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

We can use the set() method in Python to convert both into sets and then use the set. difference(s) to find the difference between two sets. Then to convert the set into a list we can use the list() method in Python.

This method cannot handle duplicate elements as the difference is calculated from the set of lists. The set allows unique elements so the duplicate elements in the list will not give the correct result.

Method 3: Using List Comprehension to Find the Difference Between Two Lists in Python

A more compact implementation of finding the difference between two lists in Python can be done using the list comprehension syntax of python.

Method 4: Finding the Difference Between Two Lists in Python Using Numpy

We can use the NumPy.concatenate() method to find the difference between two lists in python. The NumPy.concatenate() method concatenates a sequence of arrays along an existing axis.

Method 5: Finding the Difference Between Two Lists in Python Using Symmetric_difference

The symmetric difference between two lists gives the elements which are either in the first list or the second but not in both. For example , the symmetric difference between [1,2,4] and [4,7,8] is [1,2,7,8] .

symmetric_difference is used to get the elements that are either in the first set or in the second set. Firstly the list is converted to a set using the set() method in Python and then symmetric_difference is applied to the sets to find the resultant set. finally, the resultant set is converted back to a list using the list() method in Python.

This method cannot handle duplicate elements as the symmetric difference is calculated from the set of lists. The set allows unique elements so the duplicate elements in the list will not give the correct result.

Conclusion

  • Difference between two lists in Python gives the item which is in the first list but not in the second list.
  • The difference between the two lists in Python can find out using the «in» syntax of Python.
  • The list can be converted to a set and then the set difference can be used to find the difference between two sets and finally the resultant set can be converted into a list.
  • List comprehension is a more compact syntax to find the difference between two lists in Python.
  • Numpy can be used to find the difference between two lists in Python.
  • symmetric_difference can also be used to find the difference between two lists in Python.
  • Finding the difference using set and symmetric difference methods cannot handle duplicate elements in the list as these methods first convert the list to set which eliminates the duplicate elements.

Источник

Оцените статью