Programming from zero

Read values from a config dictionary

easyDictionaries Must-do

Problem statement

Database settings are stored in a dictionary: config = {"host": "db.internal", "port": 5432, "user": "app"}. Read the three values by key and print:

Connecting to db.internal:5432 as app

Examples

Example 1

Input: config = {"host": "db.internal", "port": 5432, "user": "app"}

Output: Connecting to db.internal:5432 as app

Hints

Approach

Optimal

A dictionary (dict) stores key: value pairs inside curly braces. Instead of a numeric position like a list, you look up each value by its key, for example config["host"]. This matches how configuration usually works: JSON and YAML files load into Python as dictionaries. The solution reads each key into a clearly named variable and builds the message with an f-string. Reading each value into a named variable first keeps the final line short and readable.

Python
config = {"host": "db.internal", "port": 5432, "user": "app"}
host = config["host"]
port = config["port"]
user = config["user"]
print(f"Connecting to {host}:{port} as {user}")

Follow-up questions

  • Add a "password" key and make sure your print line never includes it.
  • Loop over config.items() and print each key and value.

Frequently asked questions

The key you asked for does not exist in the dictionary. Keys must match exactly, including case. Print config.keys() to see which keys are there.