# Python – Difference between two lists

## Python – Difference between two lists

There are various ways in which the difference between two lists can be generated. In this article, we will see the two most important ways in which this can be done. One by using the set() method, and another by not using it.
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.

### By the use of set():

In this method, we convert the lists into sets explicitly and then simply reduce one from the other using the subtract operator. For more reference on set visit Sets in Python.

Example:

 `# Python code t get difference of two lists` `# Using set()` `def` `Diff(li1, li2):` `    ``return` `list``(``set``(li1) ``-` `set``(li2)) ``+` `list``(``set``(li2) ``-` `set``(li1))` `# Driver Code` `li1 ``=` `[``10``, ``15``, ``20``, ``25``, ``30``, ``35``, ``40``]` `li2 ``=` `[``25``, ``40``, ``35``]` `print``(Diff(li1, li2))`

Output :

`[10, 20, 30, 15]`

### Without using the set():

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

 `# Python code t get difference of two lists` `# Not using set()` `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` `# Driver Code` `li1 ``=` `[``10``, ``15``, ``20``, ``25``, ``30``, ``35``, ``40``]` `li2 ``=` `[``25``, ``40``, ``35``]` `li3 ``=` `Diff(li1, li2)` `print``(li3)`

Output :

`[10, 20, 30, 15]`

Last Updated on August 30, 2021 by admin

## Python – Frequency of each character in StringPython – Frequency of each character in String

Python | Frequency of each character in String Given a string, the task is to

## Python – Finding strings with given substring in listPython – Finding strings with given substring in list

Python | Finding strings with given substring in list The classical problem that can be

## OS Module in Python (with Examples)OS Module in Python (with Examples)

OS Module in Python with Examples The OS module in Python provides functions for interacting

## Pandas DataFrame.loc[]Pandas DataFrame.loc[]

Pandas DataFrame.loc attribute access a group of rows and columns by label(s) or a boolean

## Difference between loc() and iloc() in Pandas DataFrameDifference between loc() and iloc() in Pandas DataFrame

Difference between loc() and iloc() in Pandas DataFrame   Pandas library of python is very

## chr() in Pythonchr() in Python

chr() in Python The chr() method returns a string representing a character whose Unicode code point is

## Top 10 Python IDE and Code Editors in 2020Top 10 Python IDE and Code Editors in 2020

Top 10 Python IDE and Code Editors in 2020 Python… the most popular and favorite programming

## Pandas dataframe.corrwith()Pandas dataframe.corrwith()

Pandas dataframe.corrwith() Pandas dataframe.corrwith() is used to compute pairwise correlation between rows or columns of