Dictionaries and sets

Build the dict a weather tool returns, key by key, see why get() is safer than square brackets for a missing key, and use a set to drop duplicates.

9 min Free

A dictionary (type dict) maps keys to values: {"temp_c": 31, "humidity": 78}. You look a value up by its key, readings["temp_c"], rather than by position. Keys are usually strings; values can be anything, including other dicts and lists. This is exactly the shape of a JSON object, which is why every API payload, every tool argument and every tool result in agent code is a dict.

A set holds values with no duplicates and no fixed order: {"Chennai", "Mumbai"}. Its job is membership. “Is this city supported?” is one fast in test, and set(items) is the quickest way to drop repeats from a list. Both types use curly braces; the colon between key and value is what makes it a dict.

Press Run to execute weather.py. It builds the dict a weather tool would return, adds a key conditionally, updates a value, and reads it back in a loop. The second scenario shows what a missing key does when you use square brackets.

Ready
# Build the dict a weather tool returns, plus a set of supported cities.
supported = {"Chennai", "Mumbai", "Delhi", "Chennai"}
print(len(supported))

readings = {"temp_c": 31, "humidity": 78}
city = "Chennai"

report = {"status": "success", "city": city}
report["temp_c"] = readings["temp_c"]
if readings["humidity"] > 70:
    report["condition"] = "humid"
report["temp_c"] = report["temp_c"] + 1

print(report)
print(report.get("wind"), report.get("wind", "n/a"))
for key, value in report.items():
    print(key, "->", value)

print("Kochi" in supported, city in supported)
supported.add("Kochi")
print(sorted(supported))
print(report["wind"])

Press Play to run the program line by line, or step with ← →.

00/18

Variables

Nothing defined yet.

Output

Nothing printed yet.

What to notice

  1. Assigning to a key creates or replaces it. report["temp_c"] = 31 adds the key; a second assignment to the same key overwrites the value. There is no separate “add” step.
  2. [] raises, get() returns None. report["wind"] on a missing key raises KeyError and stops the program. report.get("wind") returns None, or the default you pass as the second argument. Use get() for fields that may be absent.
  3. Dicts keep insertion order. Keys come back in the order they were added, which is why the loop printed status first and condition last.
  4. .items() gives pairs. for key, value in report.items() unpacks each pair into two names. Looping over the dict itself gives only the keys.
  5. Keys must be immutable. Strings, numbers and tuples work as keys. A list does not, because it could change after it was stored.
  6. {} is an empty dict, not an empty set. Write set() for an empty set. And never rely on the order a set prints in; use sorted() when order matters.

Why this matters for AI work

When the model asks for a tool, ADK receives the arguments as a dict and calls your function with them. Your function returns a dict, and the docs recommend including a status key so the model can tell success from failure. Reading the model’s output safely means get() with a default, because a model can leave a field out. The lesson on function tools shows that round trip.

Check your understanding

1. What does report.get("wind") return when there is no "wind" key?
2. What does the expression {} create?
3. What is len({"Chennai", "Mumbai", "Chennai"})?