Calculate List Differences in Python

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13622{{"`Calculate List Differences in Python`"}} python/variables_data_types -.-> lab-13622{{"`Calculate List Differences in Python`"}} python/conditional_statements -.-> lab-13622{{"`Calculate List Differences in Python`"}} python/for_loops -.-> lab-13622{{"`Calculate List Differences in Python`"}} python/list_comprehensions -.-> lab-13622{{"`Calculate List Differences in Python`"}} python/lists -.-> lab-13622{{"`Calculate List Differences in Python`"}} python/tuples -.-> lab-13622{{"`Calculate List Differences in Python`"}} python/function_definition -.-> lab-13622{{"`Calculate List Differences in Python`"}} python/data_collections -.-> lab-13622{{"`Calculate List Differences in Python`"}} python/build_in_functions -.-> lab-13622{{"`Calculate List Differences in Python`"}} end

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]
difference([1, 2, 3], [1, 2, 4]) ## [3]

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.

Other Python Tutorials you may like