Super Simple Python: Make Your Own Contacts List

Super Simple Python is a series dedicated to creating super simple Python projects for beginners to be able to do in under 15 minutes. In this episode, we’ll be covering building your own contacts list in Python and persisting it in a JSON file in under 20 lines of code!

Video version here:

Create Our Python Contact List

To get started, we’ll import the only library we need for this, json. In the video, I don’t define this as a function, but just for practice, let’s define a function. We’ll make a function that will take our contact (expected as a dictionary) and a filename (expected as a string) and save it in a JSON file. In this function, we’ll use “a” when opening the file. The “a” parameter stands for “append” and it tells the program to append to the file if it exists, else to create the file and then add to it.

def save_contact(contact: dict, filename: str):
    with open(filename, "a") as f:
        json.dump(contact, f)

For our contacts, we’ll have four fields. Their name, email, phone number, and your relationship with them.

print("This program saves a Contact in your Contacts List")
name = input("Contact name? ")
email = input("Contact email? ")
phone = input("Contact phone? ")
relationship = input("Contact relationship? ")

After we use input to get our contact information we’ll save it into a dictionary to match the schema that we gave our save_contact function we made above.

contact = {
    "name": name,
    "email": email,
    "phone": phone,
    "relationship": relationship

Finally, we’ll call the save_contact function we made and pass it the contact dictionary and a filename we want to save our contacts in.

save_contact(contact, "contacts.json")

Check Our JSON Contact List Python

Once we run our function, we will see something like the image below. Let’s say I want to add my friend Michael Jordan to my contact list, I’d simply pass his name, his email, his phone number, and our relationship into the program and it would create an entry for me.

entries to add to contact list python
adding a contact to the json contact list

After running the program, we can open up our JSON file to make sure it ran correctly and we should see something like the image below. You’ll notice I already have Tom Brady in here, that’s because I used him for a test earlier. In the video I also add Taylor Swift (my favorite celebrity). One thing to note here is that this is not correct JSON syntax. That’s okay though, it’s still readable and that’s what matters.

    "name": "Tom Brady",
    "email": "",
    "phone": "7777777",
    "relationship": "Friend"
    "name": "Michael Jordan",
    "email": "",
    "phone": "1232323",
    "relationship": "Friend"

Further Reading

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

Leave a Reply

%d bloggers like this: