Python Tutorial

Python Project: To-Do List

Keep tasks in a list: add, show, complete, and delete items without losing the rest of the list.

What you are building

You keep a list of task titles as strings. New work is appended. Finishing a task moves it to a second list with pop. Deleting a task also uses pop, but the title is not kept. At the end you print what is still open.

Paste the examples into /try and run them. That editor is Python. It will not render HTML and it will not compile C or C++. The demo never calls input(), so the full script prints in one click.

Skills used

  • A list of strings for open tasks
  • append to add at the end
  • pop(index) to remove by position
  • enumerate so printed lines show a number the user can quote
  • A second list for completed titles

Add and show

Start empty. Append three titles. Print each with its index. Indexes start at 0, the same numberpop will use later.

Example

todo = []
todo.append("Write the report")
todo.append("Call the lab")
todo.append("Buy milk")

for i, title in enumerate(todo):
    print(i, title)

print("Count:", len(todo))

append never overwrites an earlier item. That is the point of a list: the old titles stay, the new one lands at the end.

Complete and delete by index

Completing is a move: take the title out of todo and put it on done. Deleting is a drop: take it out and ignore it. Both use pop. After a pop, later indexes shift down by one.

Example

todo = ["Write the report", "Call the lab", "Buy milk"]
done = []

title = todo.pop(1)
done.append(title)
print("Completed:", title)
print("Open:", todo)
print("Done:", done)

removed = todo.pop(1)
print("Deleted:", removed)
print("Open now:", todo)
ActionList change
Addtodo.append(title)
Completedone.append(todo.pop(i))
Deletetodo.pop(i) and discard
Show remainingprint every string still in todo

After you pop index 1, the old index 2 becomes index 1. Always print the list again before the next pop. Guessing a stale number is the usual way this program loses the wrong task.

Complete program

Helpers wrap add, show, complete, and delete. A short script of calls stands in for a menu. Run it at/try and read the remaining list at the bottom.

Example

def show(label, items):
    print(label)
    if not items:
        print("  (none)")
        return
    for i, title in enumerate(items):
        print(f"  {i}: {title}")

def add(todo, title):
    todo.append(title)
    print("Added:", title)

def complete(todo, done, index):
    if index < 0 or index >= len(todo):
        print("No task at index", index)
        return
    title = todo.pop(index)
    done.append(title)
    print("Completed:", title)

def delete(todo, index):
    if index < 0 or index >= len(todo):
        print("No task at index", index)
        return
    title = todo.pop(index)
    print("Deleted:", title)

todo = []
done = []

add(todo, "Write the report")
add(todo, "Call the lab")
add(todo, "Buy milk")
print()
show("Open tasks", todo)

print()
complete(todo, done, 1)
print()
show("Open tasks", todo)

print()
delete(todo, 1)
print()
show("Open tasks", todo)
show("Completed", done)

print()
print("Remaining:")
for title in todo:
    print("-", title)

Walk the log: three adds, complete index 1 (Call the lab), then delete the new index 1 (Buy milk). Remaining is Write the report. Completed holds Call the lab.

Common mistakes

  • Using remove(title) when two tasks share a name. remove drops the first match only. Indexes are exact.
  • Calling pop() with no argument. That removes the last item, not the one you numbered.
  • Printing 1-based numbers and then popping that same number. If you show 1, 2, 3, you mustpop(n - 1).
  • Completing with todo[index] = "done". The title is then lost and you cannot list finished work.
  • Forgetting the bounds check. pop(5) on a short list raises IndexError and the rest of the script never runs.

How to extend / Practice tasks

Three changes you can make in the complete program.

  1. Add a fourth task "Email the draft" and complete index 0 first. Confirm the remaining order is Call the lab, Buy milk, Email the draft.
  2. Print a 1-based menu (1:, 2:) but keep pop zero-based. Convert withindex = choice - 1 in complete and delete.
  3. Refuse empty titles: if title.strip() is empty, print a message and skipappend. Test it by calling add(todo, " ").

📘 Real-World Deep Dive

A to-do app is the smallest project that touches persistence: you add, list, complete, and delete items, then save them so they survive a restart. That add/list/save loop is the core of almost every CRUD app you'll ever build.

What to build

An in-memory task list with add / complete / remaining operations, structured so swapping in JSON-file storage later is a one-function change.

Real-Life Example

from dataclasses import dataclass, field

@dataclass
class TodoList:
    tasks: list[dict] = field(default_factory=list)

    def add(self, text: str) -> None:
        self.tasks.append({"text": text, "done": False})

    def complete(self, i: int) -> None:
        self.tasks[i]["done"] = True

    def remaining(self) -> list[str]:
        return [t["text"] for t in self.tasks if not t["done"]]

todo = TodoList()
todo.add("write tests"); todo.add("ship it"); todo.add("sleep")
todo.complete(0)
print("left:", todo.remaining())

Keeping storage as plain dicts/lists means saving to JSON later is literally json.dump(todo.tasks, f).

Expected Output

left: ['ship it', 'sleep']

Common mistakes

  • A shared mutable default (tasks: list = []) leaks state across every instance — use field(default_factory=list).
  • Indexing by position breaks after a delete shifts everything. Give each task a stable id once the list can be edited.
  • Writing the file on every keystroke is slow and can corrupt on a crash mid-write — save on change and write to a temp file, then rename.

🚀 Performance & Best Practices

  • For a personal list, a JSON file is perfect; reach for SQLite only when you need queries or thousands of items.
  • Filtering with a comprehension (remaining) is clear and fast enough for any human-sized list.
  • Keep the data model (TodoList) separate from the UI so a CLI, web, or GUI front-end can all reuse it.

🧪 Try It Yourself

  1. Add save(path) / load(path) with json so tasks survive a restart.
  2. Add priorities and sort remaining() by them.
  3. Give each task an id and make complete/delete work by id, not index.

FAQ: Python Project: To-Do List

Common questions about this page.

What is Python Project: To-Do List?

Python Project: To-Do List is a Python Projects lesson that explains python todo project in Python. Keep tasks in a list: add, show, complete, and delete items without losing the rest of the list. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run python todo project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn python todo project in this Python Projects Python lesson (Python Project: To-Do List).

How do I use python todo project in Python?

To use python todo project in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of python todo project?

This Python Project: To-Do List tutorial shows python todo project syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Python Project: To-Do List example for beginners

Yes. This page includes a beginner python todo project example you can copy and run. It is designed for searches such as "python todo project for beginners", "python todo project example", and "how to use python todo project".

What are common mistakes with python todo project?

Common python todo project mistakes include wrong syntax, mixing types, and skipping practice. Work through this Python Projects chapter in order, run every example, and check the output before moving on.

Why should I learn python todo project?

Python Project: To-Do List is used in real Python work. Learning python todo project helps you write clearer programs and continue the Python Projects tutorial on StudyGrid.

Is Python Project: To-Do List free to learn online?

Yes. You can learn python todo project free on StudyGrid (studygrid.in). This chapter is part of the Python Projects path and includes examples, syntax, and next-step links.