Checking List Containment in Python | Challenge

PythonPythonBeginner
Practice Now

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

Introduction

In Python, it is often necessary to check if one list is contained in another list. This can be a bit tricky, especially if the order of the elements in the lists doesn't matter. In this challenge, you will write a function that checks if the elements of one list are contained in another list, regardless of order.


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/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ErrorandExceptionHandlingGroup -.-> python/custom_exceptions("`Custom Exceptions`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-13135{{"`Checking List Containment in Python | Challenge`"}} python/booleans -.-> lab-13135{{"`Checking List Containment in Python | Challenge`"}} python/conditional_statements -.-> lab-13135{{"`Checking List Containment in Python | Challenge`"}} python/for_loops -.-> lab-13135{{"`Checking List Containment in Python | Challenge`"}} python/lists -.-> lab-13135{{"`Checking List Containment in Python | Challenge`"}} python/tuples -.-> lab-13135{{"`Checking List Containment in Python | Challenge`"}} python/function_definition -.-> lab-13135{{"`Checking List Containment in Python | Challenge`"}} python/custom_exceptions -.-> lab-13135{{"`Checking List Containment in Python | Challenge`"}} python/data_collections -.-> lab-13135{{"`Checking List Containment in Python | Challenge`"}} python/build_in_functions -.-> lab-13135{{"`Checking List Containment in Python | Challenge`"}} end

List Containment

Problem

Write a function is_contained_in(a, b) that takes two lists as arguments and returns True if all the elements of list a are contained in list b, regardless of order. Otherwise, the function should return False.

To solve this problem, you can use the following approach:

  1. Loop through each unique value in list a.
  2. For each value, check if it appears more times in list a than in list b.
  3. If any value appears more times in list a than in list b, return False.
  4. If all values in list a appear in list b at least as many times as they appear in list a, return True.

Example

assert is_contained_in([1, 4], [2, 4, 1]) == True
assert is_contained_in([1, 2, 3], [3, 2, 1]) == True
assert is_contained_in([1, 2, 3], [3, 2, 2, 1]) == False
assert is_contained_in([1, 2, 3], [4, 5, 6]) == False

Summary

In this challenge, you have learned how to check if one list is contained in another list, regardless of order. You have written a function that takes two lists as arguments and returns True if all the elements of list a are contained in list b, regardless of order.

Other Python Tutorials you may like