Test if every list element is falsy

PythonPythonBeginner
Practice Now

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

Introduction

In Python, we can use the all() function to check if all elements in a list are truthy. But what if we want to check if every element in a list is falsy? In this challenge, you will need to create a function that checks if every element in a list is falsy.


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/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13699{{"`Test if every list element is falsy`"}} python/booleans -.-> lab-13699{{"`Test if every list element is falsy`"}} python/for_loops -.-> lab-13699{{"`Test if every list element is falsy`"}} python/lists -.-> lab-13699{{"`Test if every list element is falsy`"}} python/tuples -.-> lab-13699{{"`Test if every list element is falsy`"}} python/function_definition -.-> lab-13699{{"`Test if every list element is falsy`"}} python/default_arguments -.-> lab-13699{{"`Test if every list element is falsy`"}} python/lambda_functions -.-> lab-13699{{"`Test if every list element is falsy`"}} python/build_in_functions -.-> lab-13699{{"`Test if every list element is falsy`"}} end

Test if every list element is falsy

Write a Python function called none(lst, fn = lambda x: x) that takes a list lst and an optional function fn as arguments. The function should return True if every element in the list is falsy, and False otherwise. If the optional function fn is provided, it should be used to determine the truthiness of each element in the list.

To determine if an element is falsy, you can use the same rules as Python's bool() function. In general, the following values are considered falsy:

  • False
  • None
  • 0 (integer)
  • 0.0 (float)
  • '' (empty string)
  • [] (empty list)
  • {} (empty dictionary)
  • () (empty tuple)
  • set() (empty set)

If the optional function fn is provided, it should take one argument and return a boolean value. The function will be called for each element in the list, and the return value will be used to determine the truthiness of the element.

def none(lst, fn = lambda x: x):
  return all(not fn(x) for x in lst)
none([0, 1, 2, 0], lambda x: x >= 2 ) ## False
none([0, 0, 0]) ## True

Summary

In this challenge, you learned how to create a Python function that checks if every element in a list is falsy. You also learned how to use an optional function to determine the truthiness of each element in the list.