DEV Community

Cover image for Python List Difference | Programming Tutorials | Lab
Labby for LabEx

Posted on

Python List Difference | Programming Tutorials | Lab

Introduction

MindMap

In Python, we can calculate the difference between two lists or any iterable objects. The difference between two lists is the elements that are present in the first list but not in the second list. In this challenge, you will be required to write a Python function that takes two lists as arguments and returns the difference between them.

List Difference

Write a Python function called list_difference(a, b) that takes two lists as arguments and returns the difference between them. The function should not filter out duplicate values. To solve this problem, you can follow these steps:

  1. Create a set from the second list b.
  2. Use a list comprehension on the first list a to only keep values not contained in the previously created set _b.
  3. Return the resulting list.
def difference(a, b):
  _b = set(b)
  return [item for item in a if item not in _b]
Enter fullscreen mode Exit fullscreen mode
difference([1, 2, 3], [1, 2, 4]) # [3]
Enter fullscreen mode Exit fullscreen mode

Summary

In this challenge, you have learned how to calculate the difference between two lists in Python. You have also learned how to use sets and list comprehension to solve this problem. Now, you can use this knowledge to write more efficient and concise Python code.


🚀 Practice Now: Calculate List Differences in Python


Want to Learn More?

Top comments (0)