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.
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.
# 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 ← →.
Variables
Nothing defined yet.
Output
Nothing printed yet.
What to notice
- Assigning to a key creates or replaces it.
report["temp_c"] = 31adds the key; a second assignment to the same key overwrites the value. There is no separate “add” step. []raises,get()returnsNone.report["wind"]on a missing key raisesKeyErrorand stops the program.report.get("wind")returnsNone, or the default you pass as the second argument. Useget()for fields that may be absent.- Dicts keep insertion order. Keys come back in the order they were added, which is why the loop printed
statusfirst andconditionlast. .items()gives pairs.for key, value in report.items()unpacks each pair into two names. Looping over the dict itself gives only the keys.- Keys must be immutable. Strings, numbers and tuples work as keys. A list does not, because it could change after it was stored.
{}is an empty dict, not an empty set. Writeset()for an empty set. And never rely on the order a set prints in; usesorted()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.