Symmetric Difference Between Python Lists

PythonPythonBeginner
Practice Now

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

Introduction

In Python, the symmetric difference between two sets is the set of elements that are in either of the sets, but not in their intersection. In this challenge, you will write a function that takes two lists as input and returns their symmetric difference.


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-13726{{"`Symmetric Difference Between Python Lists`"}} python/variables_data_types -.-> lab-13726{{"`Symmetric Difference Between Python Lists`"}} python/conditional_statements -.-> lab-13726{{"`Symmetric Difference Between Python Lists`"}} python/for_loops -.-> lab-13726{{"`Symmetric Difference Between Python Lists`"}} python/list_comprehensions -.-> lab-13726{{"`Symmetric Difference Between Python Lists`"}} python/lists -.-> lab-13726{{"`Symmetric Difference Between Python Lists`"}} python/tuples -.-> lab-13726{{"`Symmetric Difference Between Python Lists`"}} python/function_definition -.-> lab-13726{{"`Symmetric Difference Between Python Lists`"}} python/data_collections -.-> lab-13726{{"`Symmetric Difference Between Python Lists`"}} python/build_in_functions -.-> lab-13726{{"`Symmetric Difference Between Python Lists`"}} end

Symmetric Difference

Write a function symmetric_difference(a, b) that takes two lists as arguments and returns their symmetric difference as a list. The function should not filter out duplicate values.

To solve this problem, you can follow these steps:

  1. Create a set from each list.
  2. Use a list comprehension on each of them to only keep values not contained in the previously created set of the other.
  3. Concatenate the two lists obtained in step 2.
def symmetric_difference(a, b):
  (_a, _b) = (set(a), set(b))
  return [item for item in a if item not in _b] + [item for item in b
          if item not in _a]
symmetric_difference([1, 2, 3], [1, 2, 4]) ## [3, 4]

Summary

In this challenge, you have learned how to find the symmetric difference between two lists in Python. You have also learned how to use sets and list comprehensions to solve this problem.

Other Python Tutorials you may like