Core Data Structures

Four structures run 90% of all Python code. Master these and every LLM library will feel familiar.

▶ Watch this reel

What you'll learn

  1. list & tuple
  2. dict — the hash map
  3. set — unique & fast
  4. Comprehensions
  5. Slicing & unpacking

Remember this

list & tuple

list [1,2]tuple (1,2)
Mutable✅ append/insert/remove/sort❌ frozen
Hashable (usable as dict key)❌✅
Use forevolving collectionsfixed records

dict

set

Comprehensions

[m.upper() for m in models]                 # select
[m for m in models if "gpt" in m]           # where
[m.upper() for m in models if "gpt" in m]   # both
{m: len(m) for m in models}                 # dict comp
One line max — longer means go back to a loop.

Slicing & unpacking

Code: The four structures + comprehensions in action

models = ["gpt-4o", "claude-sonnet", "gpt-4o-mini", "llama-3"]

# list: mutable sequence
models.append("gemini-flash")
models.sort()

# tuple: immutable record
endpoint = ("api.openai.com", 443)   # host, port — fixed
host, port = endpoint                 # unpacked in one line

# dict: the structure of every LLM API response
response = {"model": "gpt-4o", "tokens": 42, "ok": True}
model = response.get("model", "unknown")
for key, value in response.items():
    print(f"  {key}: {value}")

# set: dedupe + instant membership
tags = list({"ai", "python", "ai", "rag"})   # → ['ai', 'python', 'rag']

# comprehension = LINQ in one line
gpts = [m.upper() for m in models if "gpt" in m]
lengths = {m: len(m) for m in models}

# slicing & unpacking
first, *rest = models
last_two = models[-2:]