Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Use indexes to access individual elements in a list.
  • Use indexes to modify individual elements in a list.
  • Use len() function to find the length of a list.
  • Demonstrate that lists can be changed after creation.

Lists

A list object can be used to bundle elements together in Python. A list is defined by using square brackets [] with comma separated values within the square brackets. Ex: list_1 = [1, 2, 4].

Empty lists can be defined in two ways:

  • list_1 = []
  • list_1 = list()

Lists can be made of elements of any type. Lists can contain integers, strings, floats, or any other type. Lists can also contain a combination of types. Ex: [2, "Hello", 2.5] is a valid list.

Python lists allow programmers to change the contents of the list in various ways.

Checkpoint

Lists

Concepts in Practice

Lists

1.
Which is the correct way to make a list of the numbers 3, 4, and 5?
  1. new_list == [3, 4, 5]
  2. new_list = [3, 4, 5]
  3. new_list = (3, 4, 5)
2.
Which of the following is not a valid way to specify a list in Python?
  1. my_list = [2, 2 3, 23]
  2. my_list = ["Jimmy", 2, "times"]
  3. my_list = ["C", "C++", "Python", "Java", "Rust", "Scala"]

Using indexes

Individual list elements can be accessed directly using an index. Indexes begin at 0 and end at one less than the length of the sequence. Ex: For a sequence of 50 elements, the first position is 0, and the last position is 49.

The index number is put in square brackets [] and attached to the end of the name of the list to access the required element. Ex: new_list[3] accesses the 4th element in new_list. An expression that evaluates to an integer number can also be used as an index. Similar to strings, negative indexing can also be used to address individual elements. Ex: Index -1 refers to the last element and -2 the second-to-last element.

The len() function, when called on a list, returns the length of the list.

Example 3.1

List indexes and len() function

The following code demonstrates the use of list indexes and the len() function. Line 6 shows the use of the len() function to get the length of the list. Line 10 shows how to access an element using an index. Line 14 shows how to modify a list element using an index.

1
# Setup a list of numbers
2
num_list = [2, 3, 5, 9, 11]
3
print(num_list)
4
5
# Print the length of the list
6
print("Length: ", len(num_list))
7
8
# Print the 4th element in the list
9
# The number 3 is used to refer to the 4th element
10
print("4th element:", num_list[3])
11
12
# The desired value of the 4th element is actually 7
13
# Update the value of the 4th element to 7
14
num_list[3] = 7
15
16
# The list of the first 5 prime numbers
17
print(num_list)

The above code's output is:

    [2, 3, 5, 9, 11]
    Length: 5
    4th element: 9
    [2, 3, 5, 7, 11]
    

Concepts in Practice

List indexes and the len() function

3.
Which is the correct way to access the 17th element in a list called cardList?
  1. card_list[17]
  2. 16)card_list(16)
  3. card_list[16]
4.
What is the correct way to modify the element "Tom" to "Tim" in the following list?
name_list = ["Carla", "Monique", "Westin", "Tom"]
  1. name_list[-1] = "Tim"
  2. name_list[4] = "Tim"
  3. name_list[end] = "Tim"

Try It

List basics

Write a program to complete the following:

  1. Create a list with the following elements: 2, 23, 39, 6, -5.
  2. Change the third element of the list (index 2) to 35.
  3. Print the resulting list and the list's length.
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

© Mar 15, 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.

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