Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo
Introduction to Python Programming

9.1 Modifying and iterating lists

Introduction to Python Programming9.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

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

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 permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a 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 digital format, then you must 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

© Feb 26, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.