# 2.12: List Functions

$$\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}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\avec}{\mathbf a}$$ $$\newcommand{\bvec}{\mathbf b}$$ $$\newcommand{\cvec}{\mathbf c}$$ $$\newcommand{\dvec}{\mathbf d}$$ $$\newcommand{\dtil}{\widetilde{\mathbf d}}$$ $$\newcommand{\evec}{\mathbf e}$$ $$\newcommand{\fvec}{\mathbf f}$$ $$\newcommand{\nvec}{\mathbf n}$$ $$\newcommand{\pvec}{\mathbf p}$$ $$\newcommand{\qvec}{\mathbf q}$$ $$\newcommand{\svec}{\mathbf s}$$ $$\newcommand{\tvec}{\mathbf t}$$ $$\newcommand{\uvec}{\mathbf u}$$ $$\newcommand{\vvec}{\mathbf v}$$ $$\newcommand{\wvec}{\mathbf w}$$ $$\newcommand{\xvec}{\mathbf x}$$ $$\newcommand{\yvec}{\mathbf y}$$ $$\newcommand{\zvec}{\mathbf z}$$ $$\newcommand{\rvec}{\mathbf r}$$ $$\newcommand{\mvec}{\mathbf m}$$ $$\newcommand{\zerovec}{\mathbf 0}$$ $$\newcommand{\onevec}{\mathbf 1}$$ $$\newcommand{\real}{\mathbb R}$$ $$\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}$$ $$\newcommand{\laspan}[1]{\text{Span}\{#1\}}$$ $$\newcommand{\bcal}{\cal B}$$ $$\newcommand{\ccal}{\cal C}$$ $$\newcommand{\scal}{\cal S}$$ $$\newcommand{\wcal}{\cal W}$$ $$\newcommand{\ecal}{\cal E}$$ $$\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}$$ $$\newcommand{\gray}[1]{\color{gray}{#1}}$$ $$\newcommand{\lgray}[1]{\color{lightgray}{#1}}$$ $$\newcommand{\rank}{\operatorname{rank}}$$ $$\newcommand{\row}{\text{Row}}$$ $$\newcommand{\col}{\text{Col}}$$ $$\renewcommand{\row}{\text{Row}}$$ $$\newcommand{\nul}{\text{Nul}}$$ $$\newcommand{\var}{\text{Var}}$$ $$\newcommand{\corr}{\text{corr}}$$ $$\newcommand{\len}[1]{\left|#1\right|}$$ $$\newcommand{\bbar}{\overline{\bvec}}$$ $$\newcommand{\bhat}{\widehat{\bvec}}$$ $$\newcommand{\bperp}{\bvec^\perp}$$ $$\newcommand{\xhat}{\widehat{\xvec}}$$ $$\newcommand{\vhat}{\widehat{\vvec}}$$ $$\newcommand{\uhat}{\widehat{\uvec}}$$ $$\newcommand{\what}{\widehat{\wvec}}$$ $$\newcommand{\Sighat}{\widehat{\Sigma}}$$ $$\newcommand{\lt}{<}$$ $$\newcommand{\gt}{>}$$ $$\newcommand{\amp}{&}$$ $$\definecolor{fillinmathshade}{gray}{0.9}$$

Hypothes.is tag: s20iostpy12ualr
Movies File: movies.txt

Learning Objectives

Students will be able to:

Content:

• Explain how the following functions are used with lists: reverse(), sort(), max() & min()
• explain how to use lists with files

Process:

• Write code that reverses and sorts lists
• Write code that finds the largest or smallest element of a list
• Write code that reads from a file into a list

Prior Knowledge

• Python concepts from previous activities

## Model 1: List functions

method is a function that “belongs to” an object. Some of the methods available with a list object are shown below. Some of the methods have already been used in the previous activity. To use the method, you access them with the name of the list followed by the method and arguments in parenthesis e.g. list.append(“new data").

Some Python List Methods

 append() Add an element to the end of a list Index() Returns the index of the first matched item insert() Insert an item at the defined index Count() Returns the count of the number of items passes as an argument remove() removes an item from the list Sort() Sort items in a list in ascending order pop() removes and returns an element at the index Reverse() Reverse the order of items in the list clear() removes all items from the list

Python Program 1:
letters = ['d', 'y', 'n', 'b', 'g', 'l', 'h', 'j', 'r', 'q', 't']
print(letters)
letters.reverse()
print(letters)

Critical Thinking Questions:

1.  Enter and execute Python Program 1:
1. What does the code print?
2. What does the reverse() function do?
3. Revise the Python Program 1 to use the sort() method on the letters list. Write the revised line of code here:
4. Do the list methods make a permanent change to the list? (Hint: Use the variables window in Thonny and the Debug current script run option. Step through each line of code.)
5. What happens if you add the following line of code to the program?
print(letters.sort())
1. The python predefined max() and min() functions can be used on multiple variables or with elements in a list. The ord() function will return the ASCII value for a character.
Python Program 2:
letters = ['d', 'y', 'n', 'b', 'g', 'l', 'h', 'j', 'r', 'q', 't']
print(letters)
print(max(letters))
print(min(letters))
a= max(letters)
b = min(letters)
print(ord(a))
print(ord(b))

1. Which values get returned for the max() and min() functions?
2. How does the ASCII value relate to the values returned in these functions?
3. How is the syntax of the max() and min() functions different from the list method? Which of these can be arguments in other functions?
 Information: The member operation determines if a given item is an element of a list.  The operation returns True if the item is an element of the list and False otherwise. Syntax:  in .
1. The following program demonstrates the member operation for a list. Enter and execute the program:

Python Program 3:
numbers = [23,56,13,78,56,24,89,19,5]

guess = int(input("Guess number between 1 and 100: "))

print("Here is the list of numbers: ", end= " ")
for x in numbers:
print(x, end=" ")
print()

if guess in numbers:
print("Good Job! ", guess," is in the list.")
else:
print("As you can see, ", guess, " is NOT in the list.")


1.  Circle the line of code in Python Program 3 that uses the member operation.
2. Edit the program so that if the number the user entered is not in the list, it is added to the list.  Print the list again to be sure the number is added to the list. Here is some sample output

4. This exercise will guide you through writing a program that generates 100 numbers between 1 and 500 and puts them in a list. The program prints the numbers, sorts the numbers, reverses the order of the numbers, determines the number of times a given number is in the list and prints the largest and smallest number.

1. Create an empty list to store the number
2. Create the code to generate 100 random numbers between 1 and 500 store then in the list
3. Create the code to print the list without the brackets. Create a separate function for this since you will be printing the list several times.
4. Write the code that sorts and then reverses the numbers.  (Does the order of these two lines of code matter?) Print the code after it is sorted and again after the numbers are reversed.
5. Write the code that prompts the user for a number between 1 and 500 and prints how many times that number is in the list. If the number is not in the list print the message: “Sorry, your number is not here!”
6. Print the largest and smallest number.
7. Enter and execute the code from a-f. be sure that it executes properly. Examine the sample output to the right.
8. To make the numbers appear in columns, replace the print statement in the print function with the following line of code: print("%3d" %x, end=" "). What causes the change in output format?

5.  Enter and execute the following code.

Python Program 4:
import random
outFile = open('numbers.txt','w')
for x in range(100):
outFile.write(str(random.randint(1,11)) + "\n")
outFile.close()
print("Done!")

What does the program do?

6.  Add the following code to the program above:

myList = []
inFile = open('numbers.txt','r')
for y in range(100):
inFile.close()
1. What does the program do once this code is added?
2. Explain what the following line of code does:
inFile = open('numbers.txt','r')
3. Explain what the following line of code does:
4. Add a final line of code to the program that prints the list.

This page titled 2.12: List Functions is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.