Skip to main content

Chapter 7 - Dictionaries and Structuring Data (Python)

Here's a concise walkthrough of the main ideas in Chapter 7, each with a small example.


Dictionary basics and key-value pairs

A dictionary is a mutable collection of key-value pairs, written with {}, where keys can be many types (commonly strings) and are used instead of numeric indexes.

Example:

my_cat = {'size': 'fat', 'color': 'gray', 'age': 17}
print(my_cat['size']) # 'fat'
print('My cat has ' + my_cat['color'] + ' fur.') # My cat has gray fur.

Numeric keys are allowed but are still keys, not positions:

spam = {12345: 'Luggage Combination', 42: 'The Answer'}
print(spam[12345]) # 'Luggage Combination'

Dictionaries vs lists and ordering

Lists are ordered sequences indexed by integers; dictionaries are unordered mappings indexed by keys, and key-value order doesn't matter for equality.

Example:

spam = ['cats', 'dogs', 'moose']
bacon = ['dogs', 'moose', 'cats']
print(spam == bacon) # False

eggs = {'name': 'Zophie', 'species': 'cat', 'age': '8'}
ham = {'species': 'cat', 'age': '8', 'name': 'Zophie'}
print(eggs == ham) # True

Trying to access a missing key causes KeyError:

spam = {'name': 'Zophie', 'age': 7}
# spam['color'] # KeyError: 'color'

Small project: birthday dictionary

You can use a dictionary to map names to birthdays and look them up interactively.

Example:

birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}

while True:
print('Enter a name: (blank to quit)')
name = input('>')
if name == '':
break
if name in birthdays:
print(birthdays[name] + ' is the birthday of ' + name)
else:
print('I do not have birthday information for ' + name)
print('What is their birthday?')
bday = input('>')
birthdays[name] = bday
print('Birthday database updated.')

keys(), values(), items()

keys(), values(), and items() give "list-like" views over keys, values, or key-value pairs, useful in loops and membership checks.

Example:

spam = {'color': 'red', 'age': 42}

for v in spam.values():
print(v) # red then 42

for k in spam.keys():
print(k) # color then age

for k, v in spam.items():
print('Key:', k, 'Value:', v)

Converting to real lists:

list(spam.keys())    # ['color', 'age']

Checking membership:

'color' in spam          # True (same as 'color' in spam.keys())
'red' in spam.values() # True

get() for safe access with defaults

get(key, default) returns the value for key if it exists, or default if it doesn't, avoiding KeyError.

Example:

picnic_items = {'apples': 5, 'cups': 2}
print('I am bringing ' + str(picnic_items.get('cups', 0)) + ' cups.')
print('I am bringing ' + str(picnic_items.get('eggs', 0)) + ' eggs.')

Without get, picnic_items['eggs'] would crash.


setdefault() to initialize keys

setdefault(key, default) sets dict[key] = default only if the key is missing, and returns the value for that key.

Example:

spam = {'name': 'Pooka', 'age': 5}
spam.setdefault('color', 'black')
print(spam) # {'name': 'Pooka', 'age': 5, 'color': 'black'}

spam.setdefault('color', 'white')
print(spam['color']) # still 'black'

Character frequency counter

You can combine setdefault with a loop to count occurrences of each character in a string.

Example:

message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
count = {}

for character in message:
count.setdefault(character, 0)
count[character] = count[character] + 1

print(count) # dict mapping each char to its count

Modeling real-world things: chessboard dictionary

You can model a chessboard with a dictionary whose keys are squares like 'a1'-'h8' and values are two-character piece codes like 'wK', 'bQ'.

Example board fragment:

board = {
'h1': 'bK',
'c6': 'wQ',
'g2': 'bB',
'h5': 'bQ',
'e3': 'wK',
}

Missing keys represent empty squares.


Chessboard: starting position and template

A constant dictionary holds the initial piece layout, and a large multiline BOARD_TEMPLATE string with placeholders is used to print the board.

Tiny sketch of the idea:

STARTING_PIECES = {
'a8': 'bR', 'b8': 'bN',
'a1': 'wR', 'b1': 'wN',
# ... all 32 pieces
}

WHITE_SQUARE = '||'
BLACK_SQUARE = ' '

print_chessboard builds a 64-element list of strings (pieces or blanks) by looping over ranks and files, then formats them into the template.

Core idea:

def print_chessboard(board):
squares = []
is_white_square = True
for y in '87654321':
for x in 'abcdefgh':
coord = x + y
if coord in board:
squares.append(board[coord])
else:
squares.append(WHITE_SQUARE if is_white_square else BLACK_SQUARE)
is_white_square = not is_white_square
is_white_square = not is_white_square
print(BOARD_TEMPLATE.format(*squares))

The *squares "star syntax" passes each list element as a separate argument.


Interactive chessboard commands

The main loop keeps a main_board dictionary and responds to text commands (move, remove, set, reset, clear, fill, quit) by updating the dictionary.

Examples of command handling:

import copy

main_board = copy.copy(STARTING_PIECES)

if response[0] == 'move': # move e2 e4
main_board[response[2]] = main_board[response[1]]
del main_board[response[1]]

elif response[0] == 'remove': # remove e2
del main_board[response[1]]

elif response[0] == 'set': # set e2 wP
main_board[response[1]] = response[2]

elif response[0] == 'reset':
main_board = copy.copy(STARTING_PIECES)

elif response[0] == 'clear':
main_board = {}

Nested dictionaries and lists

You can nest dictionaries inside dictionaries (and mix with lists) to represent richer structures, like guests and the items they bring to a picnic.

Example data and function:

all_guests = {
'Alice': {'apples': 5, 'pretzels': 12},
'Bob': {'ham sandwiches': 3, 'apples': 2},
'Carol': {'cups': 3, 'apple pies': 1},
}

def total_brought(guests, item):
num_brought = 0
for k, v in guests.items():
num_brought = num_brought + v.get(item, 0)
return num_brought

print('Apples', total_brought(all_guests, 'apples')) # 7
print('Cups', total_brought(all_guests, 'cups')) # 3

Overall idea of the chapter

Chapter 7 shows how dictionaries map keys to values, how to use methods like get, setdefault, keys, values, items, and how to combine lists and dictionaries to model real-world state such as chessboards or inventories in a structured, scalable way.