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:

  1. They are mutable, meaning they can be changed after creation.
  2. 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.

PYTHON

names[0]

OUTPUT

'Lovelace'

PYTHON

names[1]

OUTPUT

'Hopper'

PYTHON

names[2]

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.

PYTHON

names[-1]

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.

PYTHON

names[0: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.

PYTHON

names[:2]

OUTPUT

['Lovelace', 'Hopper']

PYTHON

names[2:]

OUTPUT

['Noether', 'Turing']
Callout

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

Challenge

Slicing practice

Using the list names, write code that returns

  1. ['Hopper', 'Noether']
  2. the last two names
  3. the number of names in the list

PYTHON

print(names[1:3])
print(names[-2:])
print(len(names))

OUTPUT

['Hopper', 'Noether']
['Noether', 'Turing']
4

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}

PYTHON

# Retrieve a value from the dictionary
authors_and_birth_years["Hopper"]

OUTPUT

1906

Keys are case sensitive. If a key does not exist, Python raises a KeyError:

PYTHON

# Does not work: key is not defined
authors_and_birth_years["HOPPER"]

ERROR

KeyError: 'HOPPER'
Challenge

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.

PYTHON

authors_and_birth_years["Hamilton"] = 1936
authors_and_birth_years["Hamilton"]

OUTPUT

1936
Key Points
  • A list stores many values in order: [value1, value2, ...].
  • Use list[index] to access an element; indexing starts at 0.
  • Use list[start:stop] to select a slice; stop is not included.
  • A dictionary stores key-value pairs: {key: value, ...}.
  • Use dictionary[key] to look up a value.