Python Arena
Step 6 of 6

Dictionaries as records

A dictionary is a good way to keep the facts about one thing together, each under a name of its own:

person = {"name": "Ada", "age": 36, "city": "London"}
print(person["name"], "is", person["age"])
person["age"] += 1
print(person)
Output
Ada is 36
{'name': 'Ada', 'age': 37, 'city': 'London'}

Compare it with a list record, as earlier in this part: ["Ada", 36, "London"]. With the list, you have to remember that index 1 is the age. With the dictionary, the key says it: person["age"]. A record can also get new facts later, person["email"] = "ada@example.com", without moving anything that's already in it. The keys are strings like any other, so spell them the same way every time: person["Age"] would stop with a KeyError.

A list of records

A list of such dictionaries, all with the same keys, is a small database:

books = [
    {"title": "The Glass Garden", "author": "Mia Stone", "pages": 312},
    {"title": "Night Trains", "author": "Leo Grant", "pages": 205},
    {"title": "Paper Moons", "author": "Mia Stone", "pages": 260},
]

for book in books:
    print(f"{book['title']} by {book['author']}")

total = 0
for book in books:
    total += book["pages"]
print("Average length:", total / len(books))
Output
The Glass Garden by Mia Stone
Night Trains by Leo Grant
Paper Moons by Mia Stone
Average length: 259.0

On each round, book is one dictionary, and book["title"] is its title. Inside an f-string that is itself in double quotes, write the key in single quotes, book['title'].

Functions make such a database easy to use. This one searches it and returns a new list of the matching titles:

def short_books(books: list, max_pages: int) -> list:
    titles = []
    for book in books:
        if book["pages"] <= max_pages:
            titles.append(book["title"])
    return titles


books = [
    {"title": "The Glass Garden", "author": "Mia Stone", "pages": 312},
    {"title": "Night Trains", "author": "Leo Grant", "pages": 205},
    {"title": "Paper Moons", "author": "Mia Stone", "pages": 260},
]
print(short_books(books, 300))
Output
['Night Trains', 'Paper Moons']

Your task

Challenge 1 of 3

Fill in the blanks so the function make_book(title, author, year) returns a book record with the keys "title", "author" and "year". For example, make_book("Night Trains", "Leo Grant", 1998) returns {'title': 'Night Trains', 'author': 'Leo Grant', 'year': 1998}.

Challenge 2

Each book is a dictionary with the keys "title", "author" and "year". Write a function oldest_title(books) that returns the title of the oldest book in the list. There is always at least one book, and no two books have the same year.

For example, for the books [{"title": "Night Trains", "author": "Leo Grant", "year": 1998}, {"title": "Paper Moons", "author": "Mia Stone", "year": 1987}], it returns "Paper Moons".

Challenge 3

Each book is a dictionary with the keys "title", "author" and "year". Write a function count_by_author(books) that returns a dictionary in which each author is a key and the value is the number of their books in the list.

books = [
    {"title": "The Glass Garden", "author": "Mia Stone", "year": 2011},
    {"title": "Night Trains", "author": "Leo Grant", "year": 1998},
    {"title": "Paper Moons", "author": "Mia Stone", "year": 1987},
]
print(count_by_author(books))
{'Mia Stone': 2, 'Leo Grant': 1}