Collection is empty | Challenge

PythonPythonBeginner
Practice Now

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

Introduction

In Python, we often need to check if a sequence or collection is empty. An empty sequence or collection is one that has no elements. In this challenge, you will create a function that checks if a value is an empty sequence or collection.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") 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-13137{{"`Collection is empty | Challenge`"}} python/booleans -.-> lab-13137{{"`Collection is empty | Challenge`"}} python/lists -.-> lab-13137{{"`Collection is empty | Challenge`"}} python/tuples -.-> lab-13137{{"`Collection is empty | Challenge`"}} python/dictionaries -.-> lab-13137{{"`Collection is empty | Challenge`"}} python/function_definition -.-> lab-13137{{"`Collection is empty | Challenge`"}} python/custom_exceptions -.-> lab-13137{{"`Collection is empty | Challenge`"}} python/data_collections -.-> lab-13137{{"`Collection is empty | Challenge`"}} python/build_in_functions -.-> lab-13137{{"`Collection is empty | Challenge`"}} end

Collection is empty

Problem

Write a Python function called is_empty(val) that takes a value as its parameter and returns True if the value is an empty sequence or collection, and False otherwise.

To check if a sequence or collection is empty, you can use the not operator to test the truth value of the provided sequence or collection. If the sequence or collection is empty, the not operator will return True.

Your function should be able to handle the following types of sequences and collections:

  • Lists
  • Tuples
  • Sets
  • Dictionaries
  • Strings
  • Ranges

Example

assert is_empty([]) == True
assert is_empty({}) == True
assert is_empty('') == True
assert is_empty(set()) == True
assert is_empty(range(0)) == True
assert is_empty([1, 2]) == False
assert is_empty({'a': 1, 'b': 2}) == False
assert is_empty('text') == False
assert is_empty(set([1, 2])) == False
assert is_empty(range(2)) == False

Summary

In this challenge, you created a function that checks if a value is an empty sequence or collection. You learned how to use the not operator to test the truth value of a sequence or collection and handle different types of sequences and collections.

Other Python Tutorials you may like