Syllabus Lesson 36 of 239 · Data Structures
Data Structures

Project: Contact Book

Time to combine everything. You will build a tiny contact book where each contact is a dict and all of them live in one list. This list-of-dicts shape is the backbone of countless real programs.

contacts = [
    {"name": "Ada", "phone": "111"},
    {"name": "Sam", "phone": "222"},
]

You will write three functions, the classic CRUD operations (create, read, delete) over that list:

  • add_contact(contacts, name, phone) appends a new dict and returns nothing
  • find_contact(contacts, name) returns the matching dict, or None if no one matches
  • delete_contact(contacts, name) removes the matching contact and returns True, or returns False if there was nothing to remove

To find, loop over the contacts and compare each contact["name"] to the name you want. Return as soon as you find a match. To delete, find it first, then use contacts.remove(match).

Your turn

Write three functions over a list of contact dicts. add_contact(contacts, name, phone) appends {"name": name, "phone": phone}. find_contact(contacts, name) returns the dict whose "name" matches, else None. delete_contact(contacts, name) removes the matching contact and returns True, or returns False when no match exists.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output