RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Python for AI — Zero to Useful
  6. /Ch. 7
Python for AI — Zero to Useful

07. Dictionaries for AI Config

Chapter 7 of 36 · 20 min
KEY INSIGHT

Dictionaries are the go-to structure for configuration and structured data. `.get()` prevents KeyError crashes. Nested dictionaries mirror JSON structure from API requests.

Why Dictionaries for AI

AI systems require configuration: model names, hyperparameters, API endpoints. Dictionaries map cleanly to JSON, the format of most AI APIs.

Creating Dictionaries

config = {
    "model": "gpt-4",
    "temperature": 0.7,
    "max_tokens": 500,
    "stop_sequences": ["###", "END"]
}

Accessing Values

model = config["model"]           # Raises KeyError if missing
model = config.get("model")       # Returns None if missing
model = config.get("model", "gpt-3.5")  # Default value if missing

Modifying Dictionaries

config["temperature"] = 0.9       # Update existing
config["api_key"] = "sk-..."      # Add new key
del config["stop_sequences"]      # Delete key

Iterating Over Dictionaries

# Keys only
for key in config:
    print(key)

# Keys and values
for key, value in config.items():
    print(f"{key}: {value}")

# Values only
for value in config.values():
    print(value)

Nested Dictionaries

request = {
    "model": "gpt-4",
    "parameters": {
        "temperature": 0.7,
        "top_p": 0.9,
        "frequency_penalty": 0.0
    },
    "messages": [
        {"role": "system", "content": "You are helpful."},
        {"role": "user", "content": "Hello."}
    ]
}

# Access nested values
temp = request["parameters"]["temperature"]
first_message = request["messages"][0]["content"]

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Create a configuration dictionary for calling an AI model. Include model name, temperature, max_tokens, and a list of stop sequences. Access each value individually and print them.

← Chapter 6
Lists and List Comprehensions
Chapter 8 →
File I/O