Back

Activity 1.20: Traversal and append()

divider

Activity 1.20

Traversal and append()

Key Concepts

Traversal

append()

A New for Loop Shape

Activity 1.16's for loops counted through range(). A for loop can also walk directly through a list, one element at a time — this is called traversal.

cities = ["Cleveland", "Chicago", "New York"]
for city in cities:
print(city)

A New for Loop Shape

Terminal window
Cleveland
Chicago
New York

When You Also Need the Index

for city in cities: gives you each value, but not its position. When you need both, loop through range(len(cities)) instead.

cities = ["Cleveland", "Chicago", "New York"]
for i in range(len(cities)):
print(i, cities[i])

When You Also Need the Index

Terminal window
0 Cleveland
1 Chicago
2 New York

Growing a List With append()

Lists don't have to start full. append() adds a new value to the end of a list — useful for building up a list one piece at a time.

tasks = []
tasks.append("Practice lists")
tasks.append("Finish homework")
print(tasks)

Growing a List With append()

Terminal window
['Practice lists', 'Finish homework']

Today's Objectives

  • Traversing a list with for item in list:
  • Traversing with both index and value using range(len(list))
  • Growing a list with append()

Key Terms

Traversal
Visiting every element of a list, one at a time.
append()
Adds a new element to the end of a list.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-20-list-traversal-and-append.


Task 1: Traverse Cities

  • Use for city in cities: to print every city, one per line.
Traversal and append()
cities = ["Cleveland", "Chicago", "New York", "Dallas", "Seattle"]
for city in cities:
print(city)

Task 2: Number the Cities

  • Use for i in range(len(cities)): to print each city with its index.
Traversal and append()
cities = ["Cleveland", "Chicago", "New York", "Dallas", "Seattle"]
for i in range(len(cities)):
print(f"{i}: {cities[i]}")

Task 3: Task List App

  • Build a menu-driven app: add a task with append(), list all tasks by traversal, or exit.
Traversal and append()
tasks = []
choice = ""
while choice != "3":
print("1) Add task")
print("2) List tasks")
print("3) Exit")
choice = input("Choose an option: ")
if choice == "1":
new_task = input("Enter a task: ")
tasks.append(new_task)
print(f"{new_task} added.")
elif choice == "2":
for task in tasks:
print(f"- {task}")
elif choice == "3":
print("Exiting.")
else:
print("Invalid option.")
divider

Checkpoint

Verify your program works correctly.

Example Output
1) Add task
2) List tasks
3) Exit
Choose an option: 1 [Enter]
Enter a task: Practice lists [Enter]
Practice lists added.
1) Add task
2) List tasks
3) Exit
Choose an option: 1 [Enter]
Enter a task: Finish homework [Enter]
Finish homework added.
1) Add task
2) List tasks
3) Exit
Choose an option: 2 [Enter]
- Practice lists
- Finish homework
1) Add task
2) List tasks
3) Exit
Choose an option: 3 [Enter]
Exiting.
divider

Reflection

Answer the following questions before submitting your work.

  1. What's the difference between for city in cities: and for i in range(len(cities)):? When would you need the second one?
  2. What does append() do to a list, and where does the new item get added?
  3. In the task list app, why does listing tasks use a for loop instead of a while loop?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete