Skip to main content

Ex 15: Numbered Task List

Problem

Build a small helper for a task manager that takes a plain list of tasks and returns them as a numbered list starting from 1.

Rules

  • Define generate_numbered_tasks(tasks: list[str]) -> list[str].
  • Use enumerate() with start=1.
  • Return a list where each item is formatted as "1. Task Name".

Boilerplate

Copy below code and paste to our IDE for head start.

# Write our code inside this function
def generate_numbered_tasks(tasks: list[str]) -> list[str]:
pass # remove this

# Call with test data
print(generate_numbered_tasks(["Clean fridge", "Thermostat check"]))

Expected Output

['1. Clean fridge', '2. Thermostat check']

Solution

Only view the solution after trying our best.

Show solution
def generate_numbered_tasks(tasks: list[str]) -> list[str]:
task_list: list[str] = []
for idx, task_name in enumerate(tasks, start=1):
task_list.append(f"{idx}. {task_name}")
return task_list

# Test call
print(generate_numbered_tasks(["Clean fridge", "Thermostat check"]))

Details

Using the start=1 parameter in enumerate() is much cleaner than manually adding 1 to the index inside the loop body (e.g., f"{idx + 1}. {task_name}").