How do I check if a list or dict is empty in EK9?

← Common Collection Tasks · Ref: Q182

EK9 provides the is empty operator and the length operator for checking collection emptiness and size.

IS EMPTY

Test whether a collection has no elements:

  if myList is empty
    stdout.println("No items")

Works on List, Dict, and String.

LENGTH

Get the number of elements:

  count <- length myList

Returns Integer.

IS NOT EMPTY

Negate the empty check:

  if ~myList is empty
    stdout.println("Has items")

CRITICAL: EMPTY IS SET

An empty collection IS set. List() of T creates a valid, set, empty list. Empty does not mean unset. The ? operator returns true for empty collections because they are meaningful values.

See Q29 for tri-state semantics. See Q45 for List basics. See Q46 for Dict basics.

Example

defines module qa.collectiontasks.emptycheck

  defines function
    getList()
      <- rtn as List of Integer: List() of Integer

  defines program
    CollectionEmptyDemo()
      stdout <- Stdout()

      // === LIST IS EMPTY ===

      emptyList <- getList()
      stdout.println(`Empty list is empty: ${emptyList is empty}`)
      stdout.println(`Empty list length: ${length emptyList}`)

      filledList <- [1, 2, 3]
      stdout.println(`Filled list is empty: ${filledList is empty}`)
      stdout.println(`Filled list length: ${length filledList}`)

      // === DICT IS EMPTY ===

      emptyDict <- Dict() of (String, Integer)
      stdout.println(`Empty dict is empty: ${emptyDict is empty}`)
      stdout.println(`Empty dict length: ${length emptyDict}`)

      filledDict <- {"a": 1, "b": 2}
      stdout.println(`Filled dict is empty: ${filledDict is empty}`)
      stdout.println(`Filled dict length: ${length filledDict}`)

      // === IS NOT EMPTY ===

      if ~filledList is empty
        stdout.println("Filled list has items")

      if emptyList is empty
        stdout.println("Empty list has no items")

      // === CRITICAL: EMPTY IS SET ===

      // Empty list IS set — it is a valid, meaningful value
      require emptyList?
      stdout.println(`Empty list isSet: ${emptyList?}`)

      // Empty dict IS set
      require emptyDict?
      stdout.println(`Empty dict isSet: ${emptyDict?}`)

      // === STRING EMPTY CHECK ===

      emptyStr <- ""
      fullStr <- "Hello"
      stdout.println(`Empty string is empty: ${emptyStr is empty}`)
      stdout.println(`Full string is empty: ${fullStr is empty}`)

Common mistakes

E50060 — EK9 uses the 'is empty' operator to check if a collection has no elements, not an isEmpty() method. See ek9 -h E50060 for details.

Incorrect:

emptyList.isEmpty()

Correct:

emptyList is empty

E50060 — EK9 uses the prefix 'length' operator to get collection size, not a size() method. See ek9 -h E50060 for details.

Incorrect:

filledList.size()

Correct:

length filledList
Other ways to ask this
  • How do I test if a collection has no elements in EK9?
  • What is the EK9 equivalent of isEmpty()?
  • How do I check the size of a collection in EK9?

Coming from another language?

Java: list.isEmpty(), list.size(). Python: len(list) == 0, not list for truthiness. Rust: vec.is_empty(), vec.len(). Go: len(slice) == 0. JavaScript: arr.length === 0. Kotlin: list.isEmpty(), list.size. EK9: list is empty, length list, empty collections are set (not unset).

Keywords: list, count, dict, set, unset, check, task, size, zero, collection, length, empty