Skip to main content
Engineering LibreTexts

8.8: Lists and Functions

  • Page ID
    8625
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    There are a number of built-in functions that can be used on lists that allow you to quickly look through a list without writing your own loops:

    >>> nums = [3, 41, 12, 9, 74, 15]
    >>> print(len(nums))
    6
    >>> print(max(nums))
    74
    >>> print(min(nums))
    3
    >>> print(sum(nums))
    154
    >>> print(sum(nums)/len(nums))
    25

    The sum() function only works when the list elements are numbers. The other functions (max(), len(), etc.) work with lists of strings and other types that can be comparable.

    We could rewrite an earlier program that computed the average of a list of numbers entered by the user using a list.

    First, the program to compute an average without a list:

    Code 8.8.1 (Python)
    total = 0
    count = 0
    while (True):
        inp = input('Enter a number: ')
        if inp == 'done': break
        value = float(inp)
        total = total + value
        count = count + 1
    
    average = total / count
    print('Average:', average)
    
    # Code: http://www.py4e.com/code3/avenum.py
    
    

    In this program, we have count and total variables to keep the number and running total of the user's numbers as we repeatedly prompt the user for a number.

    We could simply remember each number as the user entered it and use built-in functions to compute the sum and count at the end.

    Code 8.8.1 (Python)
    numlist = list()
    while (True):
        inp = input('Enter a number: ')
        if inp == 'done': break
        value = float(inp)
        numlist.append(value)
    
    average = sum(numlist) / len(numlist)
    print('Average:', average)
    
    # Code: http://www.py4e.com/code3/avelist.py
    
    

    We make an empty list before the loop starts, and then each time we have a number, we append it to the list. At the end of the program, we simply compute the sum of the numbers in the list and divide it by the count of the numbers in the list to come up with the average.


    This page titled 8.8: Lists and Functions is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Chuck Severance.

    • Was this article helpful?