Lists and Dictionaries
Real programs juggle lots of data. Lists (ordered collections) and dictionaries (labelled data) are the two workhorses you'll use in almost everything you build.
So far a variable holds one value, but real programs deal with lots of data - a list of names, the fields of a user, the items in a cart. Python's two essential collections handle this: lists for ordered sequences of things, and dictionaries for data you look up by a label (key). You'll reach for these constantly.
Lists: ordered collections
A list holds many values in order. You can access items by position, add to it, and loop over it.
fruits = ['apple', 'banana', 'cherry']
fruits[0] # 'apple' (positions start at 0)
fruits.append('date') # add to the end
len(fruits) # 4
for fruit in fruits: # loop over every item
print(fruit)list[0], the second list[1], and so on - the same zero-based counting as range(). Lists are perfect whenever you have a sequence of similar things, and they pair naturally with for loops to process each item. You can add (append), remove, and check membership ('apple' in fruits).Dictionaries: labelled data
A dictionary stores values by a key instead of a position - ideal when each piece of data has a name.
user = {'name': 'Ada', 'age': 30, 'active': True}
user['name'] # 'Ada' (look up by key)
user['age'] = 31 # update a value
user['email'] = 'a@x' # add a new key
'age' in user # True| Use a list when... | Use a dict when... |
|---|---|
| Order matters | You look things up by name |
| Items are similar | Each value has a distinct label |
| e.g. a list of scores | e.g. a user's fields |
Lists and dicts often nest - a list of dictionaries (like a list of users) is one of the most common data shapes in real programs.
Handle real data
- Lists hold ordered sequences; access by position (from 0), append, loop.
- Dicts hold labelled data; access by key, update, add keys.
- Choose by whether you look things up by position or by name.
- Nest them - a list of dicts models most real-world data.
๐ฏ Key takeaways
- Lists hold ordered collections; access items by position (from 0), append, and loop over them.
- Dictionaries store values by a key (a label) - ideal when each piece of data has a name.
- Choose a list when order matters and items are similar; a dict when you look up by name.
- Lists and dicts nest - a list of dictionaries models most real-world data.
Frequently asked
When should I use a list versus a dictionary?
Use a list when you have an ordered sequence of similar items that you'll access by position or loop through - a list of scores, names, tasks, or steps. Use a dictionary when each piece of data has a distinct label and you want to look it up by that name rather than a position - a user's fields (name, age, email), a config, or counts keyed by word. A quick test: if you'd naturally say 'the third one', that's list territory; if you'd say 'the name field' or 'the value for X', that's a dictionary. They combine constantly - a list of dictionaries (e.g. many users, each a dict of fields) is one of the most common structures in real code.
Why do list positions start at 0 instead of 1?
It's the same zero-based convention as range(): the first element of a list is at index 0, the second at index 1, and so on, so a list of length 3 has valid positions 0, 1, 2. It feels off initially but is standard across most programming languages and turns out convenient - for instance, the last item is at index len(list) - 1, and range(len(list)) gives exactly the valid indices. If you want the last item, Python also lets you use negative indices: list[-1] is the last element, list[-2] the second-to-last. You'll internalise zero-based indexing quickly; it's one of the small conventions that becomes automatic with a bit of practice.
Related lessons
Functions: Reusable Building Blocks
Functions let you name a chunk of code and reuse it. They're how you avoid repetition, orgโฆ
Python Fundamentals (Hands-On)Put It Together: Build Something Real
You now have the core building blocks โ variables, decisions, loops, functions and collectโฆ
Data & Storage with the Larz StackStorage Without a Database Server
Most projects reach for Postgres on day one - and take on a server to run, secure and backโฆ