Lists, Dicts & List Comprehensions

~20 min read

Python lists and comprehensions are the foundation of data manipulation in AI pipelines. Every tensor operation starts with understanding efficient sequence transformation.

A Python list is an ordered, mutable sequence: data = [1, 2, 3, 4]. For AI, you constantly transform lists — list comprehensions do this concisely: [x*2 for x in data if x > 1] returns [4, 6, 8]. NumPy arrays are lists with math superpowers: np.array([1,2,3]) * 2 returns array([2,4,6]) with no loops. Dictionaries map keys to values — used everywhere in AI for token vocabularies, config objects, and JSON payloads: {"model": "gpt-4", "max_tokens": 512}.

💻 Code example

# List comprehension — transform tokens
tokens = ["Hello", "World", "AI"]
lengths = [len(t) for t in tokens]  # [5, 5, 2]

# Dict comprehension — build vocab
vocab = {token: idx for idx, token in enumerate(sorted(set(tokens)))}
# {"AI": 0, "Hello": 1, "World": 2}

# NumPy vectorized math (no loops needed)
import numpy as np
embedding = np.array([0.1, 0.5, -0.3, 0.8])
normalized = embedding / np.linalg.norm(embedding)  # unit vector

💬 Deep Dive with AI

Key points

  • List comprehensions replace for-loops: [expr for item in list if condition]
  • NumPy arrays enable vectorized math — 100x faster than Python loops for numbers
  • Slicing: arr[1:4] gives elements 1, 2, 3; arr[::2] gives every other element
  • Dicts are O(1) lookup — use for vocabulary mappings and config objects

Sub-sections