0
0
PythonProgramBeginner · 2 min read

Python Program to Find Difference of Two Lists

You can find the difference of two lists in Python using list comprehension like [item for item in list1 if item not in list2] or by using set difference with list(set(list1) - set(list2)).
📋

Examples

Inputlist1 = [1, 2, 3, 4], list2 = [2, 4]
Output[1, 3]
Inputlist1 = ['apple', 'banana', 'cherry'], list2 = ['banana']
Output['apple', 'cherry']
Inputlist1 = [5, 6, 7], list2 = [8, 9]
Output[5, 6, 7]
🧠

How to Think About It

To find the difference between two lists, think about which items appear in the first list but not in the second. You check each item in the first list and keep it only if it is not found in the second list. This way, you get all unique items from the first list that are missing in the second.
📐

Algorithm

1
Get the two input lists.
2
For each item in the first list, check if it is not in the second list.
3
If the item is not in the second list, keep it.
4
Collect all such items into a new list.
5
Return or print the new list as the difference.
💻

Code

python
list1 = [1, 2, 3, 4]
list2 = [2, 4]
difference = [item for item in list1 if item not in list2]
print(difference)
Output
[1, 3]
🔍

Dry Run

Let's trace the example where list1 = [1, 2, 3, 4] and list2 = [2, 4] through the code.

1

Start with list1 and list2

list1 = [1, 2, 3, 4], list2 = [2, 4]

2

Check each item in list1 if it is not in list2

1 not in [2, 4] is True, 2 not in [2, 4] is False, 3 not in [2, 4] is True, 4 not in [2, 4] is False

3

Collect items that are True

Keep 1 and 3

4

Resulting difference list

[1, 3]

Item in list1Is item not in list2?Keep item?
1TrueYes
2FalseNo
3TrueYes
4FalseNo
💡

Why This Works

Step 1: Check membership with <code>not in</code>

The code uses item not in list2 to test if each item from the first list is missing in the second list.

Step 2: Use list comprehension to filter

List comprehension creates a new list by including only items that pass the membership test, making the code concise and readable.

Step 3: Return the filtered list

The resulting list contains only those elements unique to the first list, effectively the difference.

🔄

Alternative Approaches

Using set difference
python
list1 = [1, 2, 3, 4]
list2 = [2, 4]
difference = list(set(list1) - set(list2))
print(difference)
This method is faster for large lists but does not preserve the original order or duplicates.
Using filter and lambda
python
list1 = [1, 2, 3, 4]
list2 = [2, 4]
difference = list(filter(lambda x: x not in list2, list1))
print(difference)
This uses functional programming style but is less common and slightly less readable for beginners.

Complexity: O(n*m) time, O(n) space

Time Complexity

The list comprehension checks each item in list1 against all items in list2, leading to O(n*m) time where n and m are the lengths of the lists.

Space Complexity

A new list is created to store the difference, so space complexity is O(n) in the worst case when all items are unique.

Which Approach is Fastest?

Using set difference reduces time to O(n + m) but loses order and duplicates; list comprehension keeps order but is slower.

ApproachTimeSpaceBest For
List ComprehensionO(n*m)O(n)Preserving order and duplicates
Set DifferenceO(n + m)O(n + m)Large lists where order doesn't matter
Filter with LambdaO(n*m)O(n)Functional style preference
💡
Use list comprehension for clear and readable code when finding list differences.
⚠️
Beginners often forget that not in checks membership and try to use subtraction directly on lists, which causes errors.