Checking If Sequence Is Empty

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/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") 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/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13669{{"`Checking If Sequence Is Empty`"}} python/variables_data_types -.-> lab-13669{{"`Checking If Sequence Is Empty`"}} python/booleans -.-> lab-13669{{"`Checking If Sequence Is Empty`"}} python/lists -.-> lab-13669{{"`Checking If Sequence Is Empty`"}} python/tuples -.-> lab-13669{{"`Checking If Sequence Is Empty`"}} python/dictionaries -.-> lab-13669{{"`Checking If Sequence Is Empty`"}} python/function_definition -.-> lab-13669{{"`Checking If Sequence Is Empty`"}} python/data_collections -.-> lab-13669{{"`Checking If Sequence Is Empty`"}} python/build_in_functions -.-> lab-13669{{"`Checking If Sequence Is Empty`"}} end

Collection is empty

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
def is_empty(val):
  return not val
is_empty([]) ## True
is_empty({}) ## True
is_empty('') ## True
is_empty(set()) ## True
is_empty(range(0)) ## True
is_empty([1, 2]) ## False
is_empty({ 'a': 1, 'b': 2 }) ## False
is_empty('text') ## False
is_empty(set([1, 2])) ## False
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