Function Repository Resource:

EmptyQ

Source Notebook

Test whether a structure can be considered empty

Contributed by: Bob Sandheinrich

ResourceFunction["EmptyQ"][expr]

gives True if expr is an empty structure and False otherwise.

Details and Options

Recognized structures include List, String, Association, Dataset, Graph, SparseArray, File, CloudObject, Graphics and Databin.
A structure must exist to be considered empty.
A File object is considered empty if it is an empty file or an empty directory.

Examples

Basic Examples (3) 

Check if a list is empty:

In[1]:=
ResourceFunction["EmptyQ"][{}]
Out[1]=
In[2]:=
ResourceFunction["EmptyQ"][{1}]
Out[2]=

Check if an association is empty:

In[3]:=
ResourceFunction["EmptyQ"][<||>]
Out[3]=

If any values exist, it is not empty:

In[4]:=
ResourceFunction["EmptyQ"][<|"x" -> Missing[]|>]
Out[4]=

Scope (7) 

Any characters, including whitespace, make a string not empty:

In[5]:=
ResourceFunction["EmptyQ"] /@ {"", "a", " ", "\n"}
Out[5]=

A graph with no vertices is empty:

In[6]:=
ResourceFunction["EmptyQ"][RandomGraph[{0, 0}]]
Out[6]=
In[7]:=
ResourceFunction["EmptyQ"][RandomGraph[{10, 10}]]
Out[7]=

An empty sparse array:

In[8]:=
ResourceFunction["EmptyQ"][SparseArray[{}, {0}]]
Out[8]=

New databins are empty:

In[9]:=
ResourceFunction["EmptyQ"][bin = CreateDatabin[]]
Out[9]=
In[10]:=
DatabinAdd[bin, <|"x" -> 1|>];
ResourceFunction["EmptyQ"][bin]
Out[11]=

Use File to check if files or directories are empty:

In[12]:=
ResourceFunction["EmptyQ"][File[CreateFile[]]]
Out[12]=
In[13]:=
ResourceFunction["EmptyQ"][File[CreateDirectory[]]]
Out[13]=

Check if a CloudObject is empty:

In[14]:=
ResourceFunction["EmptyQ"][CloudPut[1]]
Out[14]=
In[15]:=
ResourceFunction["EmptyQ"][CloudExport[{}, "Byte"]]
Out[15]=

Graphics can be empty:

In[16]:=
ResourceFunction["EmptyQ"][Graphics[{}, Background -> LightBlue]]
Out[16]=

Properties and Relations (1) 

Files and cloud objects must exist to be considered empty:

In[17]:=
ResourceFunction["EmptyQ"][CloudObject[]]
Out[17]=
In[18]:=
ResourceFunction["EmptyQ"][File[CreateUUID[]]]
Out[18]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 11 October 2018

License Information