Skip to content
AI-grafen
CBuilderProgramming· about 45 min· fundamentals that rarely change· verified 2026-09-20· EN

Python — lists, loops and dictionaries

Store multiple values in lists and dictionaries, iterate through them with for-loops, and build new lists from old ones — the foundation for all data handling in Python.

Prerequisites

Code

invoice_amounts = [1200.5, 1500.0, 980.8, 1720.2]

total = 0
for amount in invoice_amounts:
    total += amount
print(total / len(invoice_amounts))     # average

large = [a for a in invoice_amounts if a > 1200]   # list comprehension

len() gives the count, invoice_amounts[0] the first item, invoice_amounts[-1] the last.

Intuition

A dictionary maps key → value. Perfect for counting:

products = ["coffee", "tea", "coffee", "juice", "coffee"]
counts = {}
for p in products:
    counts[p] = counts.get(p, 0) + 1
# {'coffee': 3, 'tea': 1, 'juice': 1}

Almost all data you encounter in AI consists of lists of numbers (vectors), lists of lists (matrices), or dictionaries (records with named fields). Getting comfortable here makes the rest much easier.

Mastery means

  • Write a for-loop that filters and sums values
  • Use a dictionary to count occurrences

Sign in to do the exercises and build your mastery up.

Sources

All the sources and licences