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
- CPython — the basicsrequired
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
Leads to
- CPython — files, CSV and JSON
- CRandomness and games in Python
- DGradient descent
- DJupyter and notebooks
- DNumPy — arrays and vectorisation
- DPyTorch — tensors and autograd
- DPython — functions, scope and exceptions
- DThe grid world: your first RL agent
- DTime complexity and big-O notation
- DTokenisation
- Dn-gram language models
- EMonte Carlo methods
Part of the goals (37)
- From blocks to Python
- The developer's toolbox
- Train an agent with reward
- Language models in practice
- Statistics for experiments
- Image classification with convolutional networks
- Build a transformer from scratch
- Train your first neural network
- Training neural networks for real
- Build a memory system for an agent
- Systems knowledge for AI engineers
- Run models more cheaply: quantisation
- Classical machine learning in practice
- Classical ML for real
- Data: collect, clean, document
- Multimodal systems
- Fine-tune and run your own models
- Build a RAG system you can trust
- Understand how generative AI works
- Build an agent you can trust
- Foundations of computer science
- Deep reinforcement learning
- AI safety in practice
- Fine-tune a model with LoRA
- The mathematics behind the models
- Frontier Lab — an independent research project
- Responsible AI in practice
- Evals in practice
- Build an NLP system end to end
- AI in production
- Generative models in depth
- An AI service in operation
- Build an AI service that survives production
- Build a voice interface
- Reproduce a paper
- Interpreting a language model
- AI, ethics and society