// python · beginner project

Python Project: Build a To-Do List App Step by Step

Build a complete command-line to-do list in Python that adds, completes and deletes tasks and saves them to a file. A beginner project that uses lists, dictionaries, functions and JSON.

Reading about lists and functions is one thing; using them together to build something real is where it all clicks. In this project you’ll build a to-do list app that runs in the terminal, lets you add, complete and delete tasks, and remembers them between runs. It uses ideas from almost every earlier guide, and each one is linked as we go.

Here’s what the finished app looks like:

output
=== CodeRunner To-Do ===
1. [ ] Revise Python lists
2. [x] Set up VS Code
3. [ ] Build the quiz game

(a)dd  (d)one  (r)emove  (q)uit: a
New task: Practise for 20 minutes
Added!

Step 1: Plan the data

Before writing code, decide how to store a task. Each task has a title and whether it’s done, so a dictionary is ideal. The whole to-do list is a list of those dictionaries:

python
tasks = [
    {"title": "Revise Python lists", "done": False},
    {"title": "Set up VS Code", "done": True},
]

Spending two minutes on this plan makes everything after it much easier.

Step 2: Show the tasks

Create a file called todo.py. Our first function prints the list with numbers and tick boxes:

python
def show_tasks(tasks):
    print("\n=== CodeRunner To-Do ===")
    if not tasks:
        print("Nothing to do. Enjoy your day!")
        return
    for number, task in enumerate(tasks, start=1):
        box = "[x]" if task["done"] else "[ ]"
        print(f"{number}. {box} {task['title']}")

if not tasks: is a neat way to check for an empty list, because an empty list counts as False. The line box = "[x]" if task["done"] else "[ ]" is a one-line if, called a conditional expression.

Step 3: Add a task

python
def add_task(tasks):
    title = input("New task: ").strip()
    if title:
        tasks.append({"title": title, "done": False})
        print("Added!")
    else:
        print("A task needs a title.")

.strip() removes stray spaces (see the strings guide), and if title: rejects an empty answer.

Step 4: Choose a task safely

Marking a task as done and deleting one both need the user to pick a task by number. That’s two places where people might type something silly, so let’s write one function that handles it properly with try and except:

python
def choose_task(tasks):
    if not tasks:
        print("There are no tasks yet.")
        return None
    try:
        number = int(input("Task number: "))
    except ValueError:
        print("Please type a number.")
        return None
    if 1 <= number <= len(tasks):
        return number - 1        # convert to a list index
    print(f"Choose a number from 1 to {len(tasks)}.")
    return None

People count from 1, but list indexes start at 0. That’s why it returns number - 1. It returns None when something is wrong, so the caller can simply skip the action.

Step 5: Complete and remove

python
def complete_task(tasks):
    index = choose_task(tasks)
    if index is not None:
        tasks[index]["done"] = True
        print("Nice work!")

def remove_task(tasks):
    index = choose_task(tasks)
    if index is not None:
        removed = tasks.pop(index)
        print(f"Removed: {removed['title']}")

We check index is not None rather than if index:, because index 0 (the first task) would count as False!

Step 6: Save and load with JSON

To keep tasks between runs we need a file. Writing a list of dictionaries by hand would be fiddly, but Python’s built-in json module turns it into text and back again in one line each:

python
import json

FILE = "tasks.json"

def load_tasks():
    try:
        with open(FILE, encoding="utf-8") as f:
            return json.load(f)
    except (FileNotFoundError, json.JSONDecodeError):
        return []

def save_tasks(tasks):
    with open(FILE, "w", encoding="utf-8") as f:
        json.dump(tasks, f, indent=2)

load_tasks returns an empty list the first time, when there’s no file yet, or if the file has somehow been damaged. indent=2 makes the saved file easy for a human to read. Open tasks.json after a run and have a look.

The name FILE is in capitals because it’s a constant: a value that never changes while the program runs.

Step 7: The main loop

Finally, tie it all together with a while loop:

python
def main():
    tasks = load_tasks()
    while True:
        show_tasks(tasks)
        choice = input("\n(a)dd  (d)one  (r)emove  (q)uit: ").strip().lower()
        if choice == "a":
            add_task(tasks)
        elif choice == "d":
            complete_task(tasks)
        elif choice == "r":
            remove_task(tasks)
        elif choice == "q":
            save_tasks(tasks)
            print("Saved. See you next time!")
            break
        else:
            print("Please choose a, d, r or q.")
        save_tasks(tasks)

if __name__ == "__main__":
    main()

The list is saved after every action, so nothing is lost even if you close the terminal window. The last two lines are explained in the modules guide.

The complete program

Put all the pieces in todo.py in this order: the import json line and FILE, then load_tasks, save_tasks, show_tasks, add_task, choose_task, complete_task, remove_task and finally main. Then run it:

terminal
python todo.py

Add a few tasks, quit, and run it again. Your tasks will still be there.

Testing it properly

Good programmers try to break their own code. Try each of these and check the app copes:

  1. Press Enter without typing a task.
  2. Type abc when asked for a task number.
  3. Type 0, or a number bigger than the list.
  4. Choose d when the list is empty.
  5. Delete the tasks.json file, then run the app again.

If any of them crash, read the traceback and fix it. That’s exactly the skill this project is meant to build.

Ideas to extend it

  • Edit a task’s title with a new (e)dit option.
  • Add due dates and show overdue tasks first (the datetime module will help).
  • Add a (c)lear option that removes all completed tasks at once. The list comprehension from the lists guide makes it one line: tasks[:] = [t for t in tasks if not t["done"]].
  • Show a count at the top, such as “2 of 5 done”.

What you practised

Lists, dictionaries, functions with parameters and return values, input validation with try/except, while loops, and saving data to a JSON file. That’s a big chunk of beginner Python in one small, genuinely useful program.

Next project: build a quiz game.