Skip to ContentGo to accessibility page
Introduction to Python Programming

9.1 Modifying and iterating lists

Introduction to Python Programming9.1 Modifying and iterating lists

9.1 Modifying and iterating lists

Learning objectives

By the end of this section you should be able to

  • Modify a list using append(), remove(), and pop() list operations.
  • Search a list using a for loop.

Using list operations to modify a list

An append() operation is used to add an element to the end of a list. In programming, append means add to the end. A remove() operation removes the specified element from a list. A pop() operation removes the last item of a list.

Example 9.1

Simple operations to modify a list

The code below demonstrates simple operations for modifying a list.

Line 8 shows the append() operation, line 12 shows the remove() operation, and line 17 shows the pop() operation. Since the pop() operation removes the last element, no parameter is needed.

1
"""Operations for adding and removing elements from a list."""
2
3
# Create a list of students working on a project
4
student_list = ["Jamie", "Vicky", "DeShawn", "Tae"]
5
print(student_list)
6
7
# Another student joins the project. The student must be added to the list.
8
student_list.append("Ming")
9
print(student_list)
10
11
# "Jamie" withdraws from the project. Jamie must be removed from the list.
12
student_list.remove("Jamie")
13
print(student_list)
14
15
# Suppose "Ming" had to be removed from the list.
16
# A pop() operation can be used since Ming is last in the list.
17
student_list.pop()
18
print(student_list)

The above code's output is:

    ['Jamie', 'Vicky', 'DeShawn', 'Tae']
    ['Jamie', 'Vicky', 'DeShawn', 'Tae', 'Ming']
    ['Vicky', 'DeShawn', 'Tae', 'Ming']
    ['Vicky', 'DeShawn', 'Tae']

Concepts in Practice

Modifying lists

1.
Which operation can be used to add an element to the end of a list?
  1. add()
  2. append()
  3. pop()
2.
What is the correct syntax to remove the element 23 from a list called number_list?
  1. remove()
  2. number_list.remove()
  3. number_list.remove(23)
3.
Which operation can be used to remove an element from the end of a list?
  1. only pop()
  2. only remove()
  3. either pop() or remove()

Iterating lists

An iterative for loop can be used to iterate through a list. Alternatively, lists can be iterated using list indexes with a counting for loop. The animation below shows both ways of iterating a list.

Checkpoint

Using len() to get the length of a list

Using len() to get the length of a list; ch 9, video 1

Concepts in Practice

Iterating lists

For the following questions, consider the list:

my_list = [2, 3, 5, 7, 9]
4.
How many times will the following for loop execute?
for element in my_list:
  1. 5
  2. 4
5.
What is the final value of i for the following counting for loop?
for i in range(0, len(my_list)):
  1. 9
  2. 4
  3. 5
6.
What is the output of the code below?
for i in range(0, len(my_list), 2):
  print(my_list[i], end=' ')
  1. 2 5 9
  2. 2 3 5 7 9
  3. 2
    5
    9

Try It

Sports list

Create a list of sports played on a college campus. The sports to be included are baseball, football, tennis, and table tennis.

Next, add volleyball to the list.

Next, remove "football" from the list and add "soccer" to the list.

Show the list contents after each modification.

Try It

Simple Searching

Write a program that prints "found!" if "soccer" is found in the given list.

Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.