Check if a List Includes All Values

PythonPythonBeginner
Practice Now

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

Introduction

In Python, you can check if all the elements in one list are included in another list. This can be useful in many situations, such as verifying if a user's input matches a list of valid options. In this challenge, you will create a function that checks if all the values in a given list are included in another list.


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/BasicConceptsGroup -.-> python/comments("`Comments`") 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`") subgraph Lab Skills python/comments -.-> lab-13656{{"`Check if a List Includes All Values`"}} python/booleans -.-> lab-13656{{"`Check if a List Includes All Values`"}} python/conditional_statements -.-> lab-13656{{"`Check if a List Includes All Values`"}} python/for_loops -.-> lab-13656{{"`Check if a List Includes All Values`"}} python/lists -.-> lab-13656{{"`Check if a List Includes All Values`"}} python/tuples -.-> lab-13656{{"`Check if a List Includes All Values`"}} python/function_definition -.-> lab-13656{{"`Check if a List Includes All Values`"}} end

Check if a List Includes All Values

Write a function called includes_all(lst, values) that takes in two lists as parameters. The function should check if all the values in the values list are included in the lst list. If all the values are included, the function should return True. If any of the values are not included, the function should return False.

To solve this problem, you should:

  1. Use a for loop to iterate through each value in the values list.
  2. Check if the current value is included in the lst list using the in operator.
  3. If the value is not included, return False.
  4. If all the values are included, return True.
def includes_all(lst, values):
  for v in values:
    if v not in lst:
      return False
  return True
includes_all([1, 2, 3, 4], [1, 4]) ## True
includes_all([1, 2, 3, 4], [1, 5]) ## False

Summary

In this challenge, you learned how to check if all the values in one list are included in another list. You used a for loop to iterate through each value in the values list and checked if it was included in the lst list using the in operator. If all the values were included, the function returned True. If any of the values were not included, the function returned False.

Other Python Tutorials you may like