Lists and Dictionaries
Last updated on 2026-10-19 | Edit this page
Overview
Questions
- How can I store many values together?
- How can I store and retrieve values by name?
Objectives
- Create lists and access their elements by index.
- Select parts of a list with slicing.
- Create dictionaries and look up values by key.
Lists
The most popular type of data collection in Python is the list. Lists have two important characteristics:
- They are mutable, meaning they can be changed after creation.
- They are heterogeneous, meaning they can store values of many different types.
To create a new list, put some values in square brackets, separated by commas. Let’s create a short list of names.
PYTHON
# Lovelace: Ada Lovelace, mathematician, wrote the first computer program
# Hopper: Grace Hopper, computing pioneer, invented the first compiler
# Noether: Emmy Noether, mathematician, pioneer of abstract algebra
# Turing: Alan Turing, foundations of computer science
names = ["Lovelace", "Hopper", "Noether", "Turing"]
Indexing
We can use index numbers to reference elements in a list. Python uses
square brackets ([ and ]) to access single
elements of objects that can be decomposed into parts. Counting starts
at 0.
OUTPUT
'Lovelace'
OUTPUT
'Hopper'
OUTPUT
'Noether'
You can also use negative indices: names[-1] is the last
element of a list, names[-2] the second last, and so
on.
OUTPUT
'Turing'
Slicing
We can also use index numbers to select a slice of a list.
The slice names[0:3] starts at index 0 and
stops before index 3.
OUTPUT
['Lovelace', 'Hopper', 'Noether']
If you leave out the start or the end, the slice starts at the beginning or goes until the end of the list.
OUTPUT
['Lovelace', 'Hopper']
OUTPUT
['Noether', 'Turing']
Strings can be indexed, too
Indexing and slicing also work on strings: "Lovelace"[0]
is 'L' and "Lovelace"[:4] is
'Love'. More on slicing: https://stackoverflow.com/questions/509211/how-slicing-in-python-works
Slicing practice
Using the list names, write code that returns
['Hopper', 'Noether']- the last two names
- the number of names in the list
Dictionaries
Whereas a list is simply a collection of elements, a dictionary stores key-value pairs. Put into plain English, it lets you store and retrieve data values by name. Dictionaries are written with curly brackets, and each key is separated from its value by a colon.
PYTHON
authors_and_birth_years = {"Lovelace": 1815,
"Hopper": 1906,
"Noether": 1882,
"Turing": 1912}
OUTPUT
1906
Keys are case sensitive. If a key does not exist, Python raises a
KeyError:
ERROR
KeyError: 'HOPPER'
Extend the dictionary
Add the computer scientist Margaret Hamilton (born 1936) to
authors_and_birth_years and then look up her birth
year.
Hint: you can assign a new value with
dictionary[key] = value.
- A list stores many values in order:
[value1, value2, ...]. - Use
list[index]to access an element; indexing starts at0. - Use
list[start:stop]to select a slice;stopis not included. - A dictionary stores key-value pairs:
{key: value, ...}. - Use
dictionary[key]to look up a value.