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 and persisting it in a JSON file in under 20 lines of code!

Video version here:

Create Our 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

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 contact to json list
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": "TB12@tb12.com",
    "phone": "7777777",
    "relationship": "Friend"
}{
    "name": "Michael Jordan",
    "email": "23@michaeljordan.com",
    "phone": "1232323",
    "relationship": "Friend"
}

Further Reading

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

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
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

%d bloggers like this: