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·Eruo Fredoline
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. 6
Python for AI — Zero to Useful

06. Lists and List Comprehensions

Chapter 6 of 36 · 20 min
KEY INSIGHT

List comprehensions replace 3-line loops with one expression. Use them for transformations and filtering. They are idiomatic Python—AI code will look foreign without them.

Creating Lists

models = ["gpt-4", "gpt-3.5-turbo", "claude-3"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", True, None]

List Operations

models.append("llama-2")      # Add to end
models.insert(0, "mistral")  # Insert at position
models.remove("gpt-4")       # Remove by value
popped = models.pop()        # Remove and return last item

Slicing

first_three = numbers[:3]     # [1, 2, 3]
last_two = numbers[-2:]      # [4, 5]
reversed_list = numbers[::-1] # [5, 4, 3, 2, 1]

List Comprehensions

Replace loops with comprehensions for cleaner code:

# Traditional loop
squares = []
for n in numbers:
    squares.append(n ** 2)

# List comprehension
squares = [n ** 2 for n in numbers]

Filtering with Comprehensions

# Filter high-temp models
models = ["gpt-4", "gpt-3.5-turbo", "claude-3", "llama-2"]
advanced_models = [m for m in models if "3" in m or "4" in m]

Nested Comprehensions

For AI data processing:

# Convert list of texts to list of token counts (simulated)
texts = ["hello", "world", "machine learning"]
token_counts = [len(text) * 1.3 for text in texts]  # rough approximation

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

Given temperatures = [0.0, 0.3, 0.5, 0.7, 0.9, 1.0], use a list comprehension to create a list of descriptions: "very low", "low", "medium", "medium-high", "high", "maximum" for each temperature.

← Chapter 5
Functions
Chapter 7 →
Dictionaries for AI Config