OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Dictionaries, sets and missing values

Use names for fields, count categories and distinguish a missing key from a stored value.

Lesson 9 of 22 · About 20 minutes · Beginner · English

By the end of this lesson

Build a frequency dictionary and use a set for membership.

Before you start

  • Python 3.12 or a compatible newer Python 3 interpreter; no external packages.
  • Complete lesson 8 in this route, or be comfortable with its examples.

Three words to know

Dictionary
A mapping from unique keys to values.
Set
A collection of unique hashable values.
Key
The identifier used to look up a dictionary value.

1. Count by key

A dictionary is useful when each value belongs to a named field or category. get supplies a fallback for a missing key. Here it supplies the initial count zero. Assigning an existing key replaces its value; dictionaries do not preserve two separate values under one identical key.

Count statusesPYTHON · save as lesson.py, then run with your interpreter
counts = {}
for status in ["open", "closed", "open"]:
    counts[status] = counts.get(status, 0) + 1
for status in sorted(counts):
    print(status, counts[status])
What you should see · example output
closed 1
open 2

2. Make missing data explicit

get without a fallback returns None for a missing key. If None itself is a meaningful stored value, use key in record to distinguish missing from present. Do not silently invent a default for a required field.

Missing versus presentPYTHON · save as lesson.py, then run with your interpreter
record = {"status": None}
print("status" in record)
print(record.get("status"))
print("name" in record)
What you should see · example output
True
None
False

3. Use sets for membership

A set removes duplicates, but its iteration order is not a display contract. Sort before producing deterministic output. For an empty set use set(); {} creates an empty dictionary.

Unique language namesPYTHON · save as lesson.py, then run with your interpreter
languages = {"Python", "Rust", "Python"}
print("Python" in languages)
print(sorted(languages))
What you should see · example output
True
['Python', 'Rust']

YOUR TURN

A small check before you continue

Count ["ok", "ok", "failed", "ok"] with a dictionary. What should an empty input produce?

Show a suggested answer

The counts are ok: 3 and failed: 1. An empty input produces an empty dictionary with this rule; it should not invent a status that never appeared.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.