    # Python find similarity between two lists

Use `collections.Counter()` perhaps; those are multi-sets, or bags, in datatype parlance:

``````from collections import Counter

counterA = Counter(listA)
counterB = Counter(listB)
``````

Now you can compare these by entries or frequencies:

``````>>> counterA
Counter({'apple': 3, 'orange': 2, 'banana': 1})
>>> counterB
Counter({'apple': 2, 'orange': 1, 'grapefruit': 1})
>>> counterA - counterB
Counter({'orange': 1, 'apple': 1, 'banana': 1})
>>> counterB - counterA
Counter({'grapefruit': 1})
``````

You can calculate their cosine similarity using:

``````import math

def counter_cosine_similarity(c1, c2):
terms = set(c1).union(c2)
dotprod = sum(c1.get(k, 0) * c2.get(k, 0) for k in terms)
magA = math.sqrt(sum(c1.get(k, 0)**2 for k in terms))
magB = math.sqrt(sum(c2.get(k, 0)**2 for k in terms))
return dotprod / (magA * magB)
``````

Which gives:

``````>>> counter_cosine_similarity(counterA, counterB)
0.8728715609439696
``````

The closer to 1 that value, the more similar the two lists are.

The cosine similarity is one score you can calculate. If you care about the length of the list, you can calculate another; if you keep that score between 0.0 and 1.0 as well you can multiply the two values for a final score between -1.0 and 1.0.

For example, to take relative lengths into account you could use:

``````def length_similarity(c1, c2):
lenc1 = sum(c1.itervalues())
lenc2 = sum(c2.itervalues())
return min(lenc1, lenc2) / float(max(lenc1, lenc2))
``````

and then combine into a function that takes the lists as inputs:

``````def similarity_score(l1, l2):
c1, c2 = Counter(l1), Counter(l2)
return length_similarity(c1, c2) * counter_cosine_similarity(c1, c2)
``````

For your two example lists, that results in:

``````>>> similarity_score(['apple', 'orange', 'apple', 'apple', 'banana', 'orange'], ['apple', 'orange', 'grapefruit', 'apple'])
0.5819143739626463
>>> similarity_score(['apple', 'apple', 'orange', 'orange'], ['apple', 'orange'])
0.4999999999999999
``````

You can mix in other metrics as needed.

View Discussion

Improve Article

Save Article

• Discuss
• View Discussion

Improve Article

Save Article

Sometimes, while working with Python list, we have a problem in which we need to find how much a list is similar to other list. The similarity quotient of both the list is what is required in many scenarios we might have. Let’s discuss a way in which this task can be performed.

Method : Using `"|" operator + "&" operator + set()`
The method which is formally applied to calculate the similarity among lists is finding the distinct elements and also common elements and computing it’s quotient. The result is then multiplied by 100, to get the percentage.

`test_list1 ``=` `[``1``, ``4``, ``6``, ``8``, ``9``, ``10``, ``7``]`

`test_list2 ``=` `[``7``, ``11``, ``12``, ``8``, ``9``]`

`print``(``"The original list 1 is : "` `+` `str``(test_list1))`

`print``(``"The original list 2 is : "` `+` `str``(test_list2))`

`res ``=` `len``(``set``(test_list1) & ``set``(test_list2)) ``/` `float``(``len``(``set``(test_list1) | ``set``(test_list2))) ``*` `100`

`print``(``"Percentage similarity among lists is : "` `+` `str``(res))`

Output :

```The original list 1 is : [1, 4, 6, 8, 9, 10, 7]
The original list 2 is : [7, 11, 12, 8, 9]
Percentage similarity among lists is : 33.33333333333333
```

A while ago I wrote a guide on how to compare two dictionaries in Python 3, and how this task is not as simple as it might sound. It turns out comparing two lists in Python is just so tricky as comparing `dict`s.

The way we've been taught to compare two objects in Python is a bit misleading. Most books and tutorials teach object comparison by using either the `==` or the `is` operator. In reality, these two operators cover just a small fraction of the most frequent use cases.

For example:

• what if we want to compare a list of floating-point numbers considering a certain tolerance?
• what if we wish to contrast two lists but ignoring the order in which the elements appear?
• maybe we need to compare two lists and return the elements that intersect both
• sometimes we might want to get the difference between two lists
• what if we have two lists of strings and need to compare them by ignoring the string cases?
• what if we're given a list of `numpy` arrays to compare each other, what can we do?
• or maybe we have a list of custom objects, or a list of dictionaries.

The list goes on and on, and for all of these use cases using `==` doesn't help.

That's what we are going to see in this article. We’ll learn the best ways of comparing two lists in Python for several use cases where the `==` operator is not enough.

## Comparing if two lists are equal in python

The easiest way to compare two lists for equality is to use the `==` operator. This comparison method works well for simple cases, but as we'll see later, it doesn't work with advanced comparisons.

An example of a simple case would be a list of `int` or `str` objects.

``````>>> numbers = [1, 2, 3]
>>> target = [1, 2, 3]
>>> numbers == target
True
>>> [1, 2, 3] == [1, 3, 2]
False
>>> ['name', 'lastname'] == ['name', 'lastname']
True
>>> ['name', 'lastname'] == ['name', 'last name']
False
``````

Pretty simple, right? Unfortunately, the world is complex, and so is production grade code. In the real world, things get complicated really fast. As an illustration, consider the following cases.

Suppose you have a list of floating points that is built dynamically. You can add single elements, or elements derived from a mathematical operation such as `0.1 + 0.1`.

``````>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
``````

Clearly, floating point arithmetic has its limitations, and sometimes we want to compare two lists but ignore precision errors, or even define some tolerance. For cases like this, the `==` operator won’t suffice.

Things can get more complicated if the lists have custom objects or objects from other libraries, such as `numpy`.

``````In : import numpy as np

In : numbers = [np.ones(3), np.zeros(2)]

In : numbers
Out: [array([1., 1., 1.]), array([0., 0.])]

In : target = [np.ones(3), np.zeros(2)]

In : numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
-5-b832db4b039d> in
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
``````

You might also like to compare the lists and return the matches. Or maybe compare the two lists and return the differences. Or perhaps you want to compare two lists ignoring the duplicates, or compare a list of dictionaries in Python.

In every single case, using `==` is not the answer, and that's what we are going to see next: how to perform complex comparison operations between two lists in Python.

## Comparing two lists of float numbers

In the previous section, we saw that floating point arithmetic can cause precision errors. If we have a list of floats and want to compare it with another list, chances are that the `==` operator won't help.

Let's revisit the example from the previous section and see what is the best way of comparing two lists of floats.

``````>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
``````

As you see, `0.1 + 0.1 + 0.1 = 0.30000000000000004`, which causes the comparison to fail. Now, how can we do better? Is it even possible?

There are a few ways of doing approaching this task. One would be to create our own custom function, that iterates over the elements and compare it one by one using the `math.isclose()` function.

Fortunately we don't have to reinvent the wheel. As I showed in the "how to compare two dicts" article, we can use a library called `deepdiff` for that. This library supports different types of objects and lists are one of them.

The example below starts off by setting up the two lists we want to compare. We then pass it to the `deepdiff.DeepDiff` constructor which returns the difference. That's great, the returned value is much more informative than a simple boolean.

Since we want to ignore the precision error, we can set the number of digits AFTER the decimal point to be used in the comparison.

The result is an empty dict, which means the lists are equal. If we try comparing a list with a float number that differs in more than 3 significant digits, the library will return that diff.

For reproducibility, in this article I used the latest version of `deepdiff` which is `5.6.0`.

``````In : from deepdiff import DeepDiff

In : numbers = []

In : numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation

In : numbers.append(0.2) # add a single element

In : target = [0.3, 0.2]

# if we don't specify the number of significant digits, the comparison will use ==
In : DeepDiff(numbers, target)
Out:
{'values_changed': {'root': {'new_value': 0.3,
'old_value': 0.30000000000000004}}}

# 0.30000000000000004 and 0.3 are equal if we only look at the first 3 significant digits
In : DeepDiff(numbers, target, significant_digits=3)
Out: {}

In : numbers
Out: [0.30000000000000004, 0.2]

In : target = [0.341, 0.2]

# 0.341 differs in more than 3 significant digits
In : DeepDiff(numbers, target, significant_digits=3)
Out:
{'values_changed': {'root': {'new_value': 0.341,
'old_value': 0.30000000000000004}}}
``````

## Comparing if two lists without order (unordered lists) are equal

Lists in Python are unordered by default. Sometimes we want to compare two lists but treat them as the same as long as they have the same elements—regardless of their order.

There are two ways of doing this:

• sorting the lists and using the `==` operator
• converting them to `set`s and using the `==` operator
• using `deepdiff`

These first two methods assume the elements can be safely compared using the `==` operator. This approach doesn’t work for floating-point numbers, and other complex objects, but as we saw in the previous section, we can use `deepdiff`.

### Sorting the lists and using the == operator

You can sort lists in Python in two different ways:

• using the `list.sort()` method
• using the `sorted()` function

The first method sorts a list in place, and that means your list will be modified. It's a good idea to not modify a list in place as it can introduce bugs that are hard to detect.

Using `sorted` is better since it returns a new list and keep the original unmodified.

Let's see how it works.

``````In : numbers = [10, 30, 20]

In : target = [10, 20, 30]

In : numbers == target
Out: False

In : sorted(numbers) == sorted(target)
Out: True

In : sorted(numbers)
Out: [10, 20, 30]

In : sorted(target)
Out: [10, 20, 30]
``````

As a consequence, by sorting the lists first we ensure that both lists will have the same order, and thus can be compared using the `==` operator.

### Converting the lists to a set

Contrary to lists, sets in Python don’t care about order. For example, a set `{1, 2, 3}` is the same as `{2, 3, 1}`. As such, we can use this feature to compare the two lists ignoring the elements’ order.

To do so, we convert each list into a set, then using the `==` to compare them.

``````In : numbers = [10, 30, 20]

In : target = [10, 20, 30]

In : set(numbers) == set(target)
Out: True

In : set(numbers)
Out: {10, 20, 30}

In : set(target)
Out: {10, 20, 30}
``````

### Using the deepdiff library

This library also allows us to ignore the order in sequences such as `list`s. By default, it will take the order in consideration, but if we set `ignore_order` to `True`, then we're all good. Let's see this in action.

``````In : numbers = [10, 30, 20]

In : target = [10, 20, 30]

In : DeepDiff(numbers, target)
Out:
{'values_changed': {'root': {'new_value': 20, 'old_value': 30},
'root': {'new_value': 30, 'old_value': 20}}}

In : DeepDiff(numbers, target, ignore_order=True)
Out: {}
``````

Using `deepdiff` has pros and cons. In the end, it is an external library you need to install, so if you can use a `set` to compare the lists, then stick to it. However, if you have other use cases where it can shine, then I’d go with it.

## How to compare two lists and return matches

In this section, we'll see how we can compare two lists and find their intersection. In other words, we want to find the values that appear in both.

To do that, we can once more use a `set` and take their intersection.

``````In : t1 = [2, 1, 0, 7, 4, 9, 3]

In : t2 = [7, 6, 11, 12, 9, 23, 2]

In : set(t1).intersection(set(t2))
Out: {2, 7, 9}

# the & operator is a shorthand for the set.intersection() method
In : set(t1) & set(t2)
Out: {2, 7, 9}
``````

## How to compare two lists in python and return differences

We can the find difference between two lists in python in two different ways:

• using `set`
• using the`deepdiff` library

### Using `set`

Just like we did to determine the intersection, we can leverage the `set` data structure to check difference between two lists in python.

If we want to get all the elements that are present in the first list but not in the second, we can use the `set.difference()`.

On the other hand, if we want to find all the elements that are in either of the lists but not both, then we can use `set.symmetric_difference()`.

``````In : t1 = [2, 1, 0, 7, 4, 9, 3]

In : t2 = [7, 6, 11, 12, 9, 23, 2]

In : set(t1).difference(set(t2))
Out: {0, 1, 3, 4}

In : set(t2).difference(set(t1))
Out: {6, 11, 12, 23}

In : set(t1).symmetric_difference(set(t2))
Out: {0, 1, 3, 4, 6, 11, 12, 23}

In : set(t1) - set(t2)
Out: {0, 1, 3, 4}

In : set(t1) ^ set(t2)
Out: {0, 1, 3, 4, 6, 11, 12, 23}
``````

This method has a limitation: it groups what is different between the lists into one final result which is the set difference. What if we want to know which elements in that diff belong to what list?

### Using deepdiff

As we've seen so far, this library is powerful and it returns a nice diff. Let's see what happens when we use `deepdiff` to get the difference between two lists in Python.

``````In : t1 = [2, 1, 0, 7, 4, 9, 3]

In : t2 = [7, 6, 11, 12, 9, 23, 2]

In : DeepDiff(t1, t2)
Out:
{'values_changed': {'root': {'new_value': 7, 'old_value': 2},
'root': {'new_value': 6, 'old_value': 1},
'root': {'new_value': 11, 'old_value': 0},
'root': {'new_value': 12, 'old_value': 7},
'root': {'new_value': 9, 'old_value': 4},
'root': {'new_value': 23, 'old_value': 9},
'root': {'new_value': 2, 'old_value': 3}}}

In : DeepDiff(t1, t2, ignore_order=True)
Out:
{'values_changed': {'root': {'new_value': 6, 'old_value': 4},
'root': {'new_value': 11, 'old_value': 3},
'root': {'new_value': 12, 'old_value': 1}},
'iterable_item_removed': {'root': 0}}
``````

Accordingly, `deepdiff` returns what changed from one list to the other. The right approach then will depend on your use case. If you want a detailed diff, then use `DeepDiff`. Otherwise, just use a `set`.

## How to compare two lists of strings

Comparing two lists of string in Python depends largely on what type of comparison you want to make. That's because we can compare a string in a handful of ways.

In this section, we'll see 3 different ways of doing that.

The simplest one is using a `==` operator, like we saw in the beginning. This method is suitable if you want a strict comparison between each string.

``````In : names = ['jack', 'josh', 'james']

In : target = ['jack', 'josh', 'james']

In : names == target
Out: True
``````

Things start to get messy if you want to compare the list of strings but ignoring the case. Using the `==` for that just doesn't work.

``````In : names = ['Jack', 'Josh', 'James']

In : target = ['jack', 'josh', 'james']

In : names == target
Out: False
``````

The best tool for that is again `deepdiff`. It allows us to ignore the string by passing a boolean flag to it.

``````In : import deepdiff

In : names = ['Jack', 'Josh', 'James']

In : target = ['jack', 'josh', 'james']

# ignoring string case
In : deepdiff.DeepDiff(names, target, ignore_string_case=True)
Out: {}

# considering the case
In : deepdiff.DeepDiff(names, target)
Out:
{'values_changed': {'root': {'new_value': 'jack', 'old_value': 'Jack'},
'root': {'new_value': 'josh', 'old_value': 'Josh'},
'root': {'new_value': 'james', 'old_value': 'James'}}}
``````

We can also ignore the order in which the strings appear in the lists.

``````In : names = ['Jack', 'James', 'Josh']

In : target = ['jack', 'josh', 'james']

# ignoring the order and string case
In : deepdiff.DeepDiff(names, target, ignore_string_case=True, ignore_order=T
...: rue)
Out: {}

# considering the order but ignoring the case
In : deepdiff.DeepDiff(names, target, ignore_string_case=True)
Out:
{'values_changed': {'root': {'new_value': 'josh', 'old_value': 'james'},
'root': {'new_value': 'james', 'old_value': 'josh'}}}
``````

You can also go further and perform advanced comparisons by passing a custom operator to `DeepDiff`.

For example, suppose you want to compare the strings but ignoring any whitespace they may have.

Or perhaps you want to perform a fuzzy matching using an edit distance metric.

To do that, we can write the comparison logic in the operator class and pass it to `DeepDiff`.

In this first example, we'll ignore any whitespace by trimming the strings before comparing them.

``````class IgnoreWhitespaceOperator:

def match(self, level) -> bool:
return True

def give_up_diffing(self, level, diff_instance) -> bool:
if isinstance(level.t1, str) and isinstance(level.t2, str):
return level.t1.strip() == level.t2.strip()
return False
``````

Then we can just plug into `DeepDiff` by adding it to the list of `custom_operators`, like so `custom_operators=[IgnoreWhitespaceOperator()]`.

``````In : from deepdiff import DeepDiff

In : names = ['Jack', 'James ', '  Josh ']

In : target = ['Jack', 'James', 'Josh',]

# the operator will ignore the spaces in both lists
In : DeepDiff(names, target, custom_operators=[IgnoreWhitespaceOperator()])
Out: {}

In : target = ['Jack', 'James', 'Josh', 'Jelly']

# if one of the list has an additional member, this will be flagged
In : DeepDiff(names, target, custom_operators=[IgnoreWhitespaceOperator()])

In : target = ['Jack', 'Josh', 'James']

# by default, the library doesn't ignore order
In : DeepDiff(names, target, custom_operators=[IgnoreWhitespaceOperator()])
Out:
{'values_changed': {'root': {'new_value': 'Josh', 'old_value': 'James '},
'root': {'new_value': 'James', 'old_value': '  Josh '}}}

# if you don't care about order, be explicit
In : DeepDiff(names, target, ignore_order=True, custom_operators=[IgnoreWhitespaceOperator()])
Out: {}
``````

## How to compare two lists of dictionaries

Comparing two lists of dictionaries in Python is definitely intricate without the help of an external library. As we've seen so far, `deepdiff` is versatile enough and we can use it to compare deep complex objects such as lists of dictionaries.

Let's see what happens when we pass two lists of dictionaries.

``````In : from deepdiff import DeepDiff

In : first_list = [
...:     {
...:         'number': 1,
...:         'list': ['one', 'two']
...:     },
...:     {
...:         'number': 2,
...:         'list': ['one', 'two']
...:     },
...: ]

In : target_list = [
...:     {
...:         'number': 3,
...:         'list': ['one', 'two']
...:     },
...:     {
...:         'number': 2,
...:         'list': ['one', 'two']
...:     },
...: ]

In : DeepDiff(first_list, target_list)
Out: {'values_changed': {"root['number']": {'new_value': 3, 'old_value': 1}}}
``````

It outputs the exact location where the elements differ and what the difference is!

Let's see another example where a list has a missing element.

``````In : first_list = [
...:     {
...:         'number': 1,
...:         'list': ['one', 'two']
...:     },
...:     {
...:         'number': 2,
...:         'list': ['one', 'two']
...:     },
...: ]

In : target = [
...:     {
...:         'number': 3,
...:         'list': ['one', 'two']
...:     },
...: ]

In :

In : DeepDiff(first_list, target)
Out:
{'values_changed': {"root['number']": {'new_value': 3, 'old_value': 1}},
'iterable_item_removed': {'root': {'number': 2, 'list': ['one', 'two']}}}
``````

It says the the second dictionary has been removed, which is the case for this example.

## How to compare two list of lists

Comparing multidimensional lists—a.k.a list of lists—is easy for `deepdiff`. It works just like a list of `dict`s.

In the example below, we have two multidimensional lists that we want to compare. When passed to `DeepDiff`, it returns the exact location in which the elements differ.

For example, for the position ``, the new value is 8, and the old is 3. Another interesting aspect is that it works for deeply nested structures, for instance, `deepdiff` also highlights the difference in the `` position.

``````In : from deepdiff import DeepDiff

In : first_list = [[1, 2], [3, 4], []]

In : target_list = [[1, 2], [8, 4], []]

In : DeepDiff(first_list, target_list)
Out:
{'values_changed': {'root': {'new_value': 8, 'old_value': 3},
'root': {'new_value': 7, 'old_value': 5}}}
``````

When feeding the library with two identical multidimensional lists, it returns an empty response.

``````In : target_list = [[1, 2], [8, 4], []]

In : second_list = [[1, 2], [8, 4], []]

In : DeepDiff(second_list, target_list)
Out: {}
``````

## How to compare two lists of objects

Sometimes we have a list of custom objects that we want to compare. Maybe we want to get a diff, or just check if they contain the same elements. The solution for this problem couldn't be different: use `deepdiff`.

The following example demonstrates the power of this library. We're going to compare two lists containing a custom objects, and we'll be able to assert if they are equal or not and what are the differences.

In the example below, we have two lists of `Person` objects. The only difference between the two is that in the last position `Person` object has a different age. `deepdiff` not only finds the right position - `` - but also finds that `age` field is different as well.

``````In : from deepdiff import DeepDiff

In : first = [Person('Jack', 34), Person('Janine', 23)]

In : target = [Person('Jack', 34), Person('Janine', 24)]

In : DeepDiff(first, target)
Out: {'values_changed': {'root.age': {'new_value': 24, 'old_value': 23}}}

In : second = [Person('Jack', 34), Person('Janine', 24)]

In : DeepDiff(second, target)
Out: {}
``````

## How to compare two lists of numpy arrays

In this section, we'll see how to compare two lists of `numpy` arrays. This is a fairly common task for those who work with data science and/or machine learning.

We saw in the first section that using the `==` operator doesn't work well with lists of `numpy`arrays. Luckily we can use... guess what!? Yes, we can use `deepdiff`.

The example below shows two lists with different `numpy` arrays and the library can detect the exact position in which they differ. How cool is that?

``````In : import numpy as np

In : from deepdiff import DeepDiff

In : first = [np.ones(3), np.array([1, 2, 3])]

In : target = [np.zeros(4), np.array([1, 2, 3, 4])]

In : DeepDiff(first, target)
Out:
{'values_changed': {'root': {'new_value': 0.0, 'old_value': 1.0},
'root': {'new_value': 0.0, 'old_value': 1.0},
'root': {'new_value': 0.0, 'old_value': 1.0}},
``````

## Conclusion

In this post, we saw many ways to compare two lists in Python. The best method depends on what kind of elements we have and how we want to compare. Hopefully, you now know how to:

• check if two lists are equal in python
• compare two lists without order (unordered lists)
• compare two lists in python and return matches
• compare two lists in python and return differences
• compare two lists of strings
• compare two lists of dictionaries
• compare two list of lists
• compare two lists of objects
• compare two lists of numpy arrays

Other posts you may like:

• The Best Way to Compare Two Dictionaries in Python

• How to Compare Two Strings in Python (in 8 Easy Ways)

• 7 Different Ways to Flatten a List of Lists in Python

See you next time!

This post was originally published at https://miguendes.me

### How do you find the similarities between two lists in Python?

sort() and == operator. The 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.

### How do you find the similarity between two lists?

Method : Using "|" operator + "&" operator + set() The method which is formally applied to calculate the similarity among lists is finding the distinct elements and also common elements and computing it's quotient. The result is then multiplied by 100, to get the percentage.

### How do I compare two lists and differences in Python?

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.

### How do you find the cosine similarity between two lists?

Use the sklearn Module to Calculate the Cosine Similarity Between Two Lists in Python. In the sklearn module, there is an in-built function called cosine_similarity() to calculate the cosine similarity. 