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

9.3 Common list operations

Introduction to Python Programming9.3 Common list operations

Learning objectives

By the end of this section you should be able to

  • Use built-in functions max(), min(), and sum().
  • Demonstrate how to copy a list.

Using built-in operations

The max() function called on a list returns the largest element in the list. The min() function called on a list returns the smallest element in the list. The max() and min() functions work for lists as long as elements within the list are comparable.

The sum() function called on a list of numbers returns the sum of all elements in the list.

Example 9.3

Common list operations

    """Common list operations."""

    # Set up a list of number
    snum_list = [28, 92, 17, 3, -5, 999, 1]

    # Set up a list of words
    city_list = ["New York", "Missoula", "Chicago", "Bozeman", 
    "Birmingham", "Austin", "Sacramento"]

    # Usage of the max() funtion
    print(max(num_list))

    # max() function works for strings as well
    print(max(city_list))
    
    # Usage of the min() funtion which also works for strings
    print(min(num_list))

    print(min(city_list))

    # sum() only works for a list of numbers
    print(sum(num_list))
    

The above code's output is:

    999
    Sacramento
    -5
    Austin
    1135
    

Concepts in Practice

List operations

1.
What is the correct way to get the minimum of a list named nums_list?
  1. min(nums_list)
  2. nums_list.min()
  3. minimum(nums_list)
2.
What is the minimum of the following list?
["Lollapalooza", "Coachella", "Newport Jazz festival", "Hardly Strictly Bluegrass", "Austin City Limits"]
  1. Coachella
  2. Austin City Limits
  3. The minimum doesn't exist.
3.
What value does the function call return?
sum([1.2, 2.1, 3.2, 5.9])
  1. sum() only works for integers.
  2. 11
  3. 12.4

Copying a list

The copy() method is used to create a copy of a list.

Checkpoint

Copying a list

Concepts in Practice

Copying a list

4.
What is the output of the following code?
my_list = [1, 2, 3]
list2 = my_list
list2[0] = 13
print(sum(my_list))
  1. 6
  2. 13
  3. 18
5.
What is the output of the following code?
my_list = [1, 2, 3]
list2 = my_list.copy()
list2[0] = 13
print(max(my_list))
  1. 3
  2. 13
  3. 18
6.
What is the output of the following code?
my_list = ["Cat", "Dog", "Hamster"]
list2 = my_list
list2[2] = "Pigeon"
print(sum(my_list))
  1. CatDogPigeon
  2. Error

Try It

Copy

Make a copy of word_list called wisdom. Sort the list called wisdom. Create a sentence using the words in each list and print those sentences (no need to add periods at the end of the sentences).

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.