Dictionaries

Last updated on 2025-03-18 | Edit this page

Overview

Questions

  • How is a dictionary different from a list?

Objectives

  • Explain why keyed data objects exist.
  • Write programs that store values in dictionaries, modify them, and iterate over them.

A dictionary stores values under keys.


  • Sometimes we want to store pairs of related values
  • A list of lists would be difficult to manage
  • Use a dictionary to store values “keyed” by a unique value
    • Defined by curly braces {...}
    • keys and values are separated by a colon :
    • key/value pairs are separated by commas ,
  • A Value can by any kind of data type (string, int, even lists or other dicts)

PYTHON

inventory = {
    "apples": 12,
    "bananas": 4,
    "coconuts": 0
}
print('inventory:', inventory)
print("items in inventory:", len(inventory))

OUTPUT

apples: 12
items in inventory: 3

Use a key to fetch the corresponding value from the dictionary


  • A key has exactly one value associated with it.
  • Keys must be unique within the dictionary.
    • Values do not have to be unique

PYTHON

print('number of apples:', inventory['apples'])
print('number of coconuts:', inventory['coconuts'])

OUTPUT

number of apples: 12
number of coconuts: 0

A value can be replaced by assinging to it


  • Since each key can only exist once in the dictionary, assinging a value to that key will overwrite any existing values for that key.

PYTHON

inventory['apples'] = 7
print('number of apples is now:', inventory['apples'])

OUTPUT

number of apples is now: 7

Use a key to add a new value to a dictionary


  • If a key does not already exist in the dictionary, then assigning to that key will create a new entry in the dictionary.

PYTHON

print('inventory is now:', inventory)
inventory["oranges"] = 24
print('inventory is now:', inventory)

OUTPUT

inventory is now: {'apples': 7, 'bananas': 4, 'coconuts': 0}
inventory is now: {'apples': 7, 'bananas': 4, 'coconuts': 0, 'oranges': 24}

Use del to remove a key-value pair from a dictionary


  • Use the del statement to remove a key-value pair from a dictionary

PYTHON

del inventory['bananas']
print('inventory is now:', inventory)

OUTPUT

inventory is now: {'apples': 7, 'coconuts': 0, 'oranges': 24}

Keys are unique


  • If you assign a value to an existing key, it replaces the existing value

PYTHON

inventory['oranges'] = 100
print('inventory is now:', inventory)

OUTPUT

inventory is now: {'apples': 7, 'coconuts': 0, 'oranges': 100}

Dictionaries may contain values of different types


  • There is no restriction on the types of values a dictionary may contain
  • You can mix values in the same dictionary

PYTHON

inventory['limes'] = "Out of Stock"
print('inventory is now', inventory)

OUTPUT

inventory is now: {'apples': 7, 'coconuts': 0, 'oranges': 100, 'limes': 'Out of Stock'}

Requesting a key that doesn’t exist is an error


  • Python will report a KeyError if we attempt to access a key that doesn’t exist

PYTHON

print('number of pears:', inventory['pears'])

OUTPUT

KeyError: 'pears'

We can check it a key exists before using it


  • Use the in operator to check if a key exists in a dictionary

PYTHON

print('inventory is now:', inventory)
print('is there a key for oranges?', 'oranges' in inventory)
print('is there a key for pears?', 'pears' in inventory)

OUTPUT

inventory is now: {'apples': 7, 'coconuts': 0, 'oranges': 100, 'limes': 'Out of Stock'}
is there a key for oranges? True
is there a key for pears? False

We can get a list of all the keys / values in a dictionary


  • Use the .keys() method to get a list of all the keys in a dictionary
  • Use the .values() method to get a list of all the values in a dictionary
    • these methods return a “dict_keys” or “dict_values” object, which behave similarly to lists

PYTHON

print("keys in inventory:", inventory.keys())
print("values in inventory:", inventory.values())

OUTPUT

keys in inventory: dict_keys(['apples', 'coconuts', 'oranges', 'limes'])
values in inventory: dict_values([7, 0, 100, 'Out of Stock'])
Challenge

Fill in the Blanks

Fill in the blanks so that the program below produces the output shown.

PYTHON

prices = {
    "laptop": ___,
    ______: ___,
    ______: 85,
}
print('all prices', prices)
print('price of mouse:', prices[______])
prices[______] = 50
print('all prices', prices)

OUTPUT

all prices {'laptop': 500, 'mouse': 10, 'camera': 85}
price of mouse: 10
all prices {'laptop': 500, 'mouse': 10, 'camera': 85, 'printer': 50}

PYTHON

prices = {
    "laptop": 500,
    "mouse": 10,
    "camera": 85
}
print('all prices', prices)
print('price of mouse:', prices['mouse'])
prices["printer"] = 50
print('all prices', prices)
Challenge

Adding Two Values

Given the following dictionary, how would we get the cost of a laptop and a camera?

PYTHON

prices = {
    "laptop": 500,
    "mouse": 10,
    "camera": 85
}

OUTPUT

price of a laptop + camera: 585

PYTHON

prices = {
    "laptop": 500,
    "mouse": 10,
    "camera": 85
}
print('price of a laptop + camera:', prices['laptop'] + prices['camera'])
Challenge

Summing Values

Given the following dictionary, how could we calculate the total cost of all items?

Hint: try the sum function

PYTHON

prices = {
    "laptop": 500,
    "mouse": 10,
    "camera": 85,
    "printer": 50
}

OUTPUT

total cost: 645

PYTHON


prices = {
    "laptop": 500,
    "mouse": 10,
    "camera": 85,
    "printer": 50
}

print('total cost:', sum(prices.values()))