# 2.11: Lists

hypothes.is tag: s20iostpy11ualr

## Learning Objectives

Students will be able to:

Content:

• Define a list
• Identify elements of a list
• Explain the purpose of positive and negative indexes in a list.
• Explain how to access individual elements of a list
• Explain how the following functions are used with lists: append(), insert(), remove(), count(), index()
• Explain how to replace an item

Process:

• Write code that prints a list
• Write code that edits a list – add, remove, and insert items

Prior Knowledge

• Python concepts from all previous activities

## Model 1: Lists

A sequence is an object that stores multiple data items in a contiguous manner. Two types of sequences are strings and lists. Lists are special variables that store multiple values. Each value stored in a list is called an element or item. The following are examples of lists.

 Examples of Lists in Python digits = [0,1,2,3,4,5,6,7,8,9] compoundClass = ["alkanes", "alkenes", "alcohols", "ketones", "alkyl halides"] elementalData = ['hydrogen', 1, 1.008, 'helium', 2, 4.00, 'lithium', 3, 6.94] 

The location of an element or item in a list is its index number.  Index numbers begin at 0. A list having 5 elements will have index values from 0 to 4. The syntax for accessing the elements of a list is the same as for accessing the characters of a string---the bracket operator. The expression inside the brackets specifies the index.

 Python Program 11.1 Output letters = ['a','b','c','d','e'] data= letters[3] print(data) print(type(letters[3]))  d

Critical Thinking Questions

1. Examine the sample lists in the Model.
1. How many elements does the list named digits contain?
2. What type(class) of data is stored in each list (String, integer, float)?
• digits list:
• compoundClass list:
• elementalData list:
3. How would you define a list?
4. Why is the output for program 1 “d”, when the third element in the list appears to be “c”?
5. What is the value stored in elementalData[2]?

2. Enter the following lines of code into your Thonny editor window.

 Python Program 11.2 compoundClass = ["alkanes", "alkenes", "alcohols", "ketones", "alkyl halides"] print(compoundClass[3]) 
1.  How would you change the code so that the output is “alkyl halides”? compoundClass[5]. What is printed? Why?
2.  Edit the print statement in the programs so that it prints compoundClass[5]. What is printed? Why?
3.  Edit the print statement in the programs so that it prints compoundClass[-1]. What is printed?
4.  Change -1 to -2 in “c.” above. What is printed?
5.  Explain how the positive and negative indexes locate the desired elements.
6.  What is printed with the following print statement:
print(compoundClass)

How is the information displayed?

3. Enter and execute the following code:

 Python Program 11.3 gradebook = ["Abbot", 78, 89, "Barrava",97,86] print(gradebook) for x in gradebook: print(x, end = " ") print() 
1.  What is the output for the second line of code: print(gradebook)?
2.  Examine the following code. It contains a FOR loop but does not use the range() function. In previous FOR loops the values resulting from the range() function were stored in x during each iteration of the loop
for x in gradebook:
print(x, end = " ")
print()
• What is being stored in x for each interation of the loop in the following code?
• What is the output of this code?
1.  What are the similarities and differences between the output for “a.” and “b.”?
2.  Add each of the following print statements to the code above.  What is the output for each statement?  Explain the output.
print((gradebook[1]+gradebook[2])/2)
print(gradebook[2]+gradebook[3])

4. Enter and execute the following code:

 Python Program 14.4 elements = ['hydrogen', 'helium' ,'lithium', 'beryllium', 'boron', 'carbon'] print(elements) elements.append('nitrogen') print(elements) 

a.  Explain what the following line of code does:

elements.append(‘nitrogen’)

b.  Write a line of code that would add the element oxygen to the list.

5. Enter and execute the following code:

 Python Program 11.5 elements = ['helium','beryllium','boron','carbon','nitrogen'] print(elements) elements.insert(1,'lithium') print(elements) 
1.  Explain what the following line of code does:
elements.insert(1,‘lithium’)
1.  Write a line of code that would place the element: hydrogen at the beginning of the list.

6.  Enter and execute the following code:

 Python Program 11.6 elements = ['helium','beryllium','lithium','boron','carbon','nitrogen'] print(elements) del elements[2] print(elements) 
1.  Explain what the following line of code does:
del elements[2]
1.  Write a line of code that would delete the last element in the list.

7.  Enter and execute the following code:

 Python Program 11.7 elements = ['hydrogen', 'helium','lithium','beryllium','boron','carbon','nitrogen'] print(elements) elements.remove('lithium') print(elements) 
1.  Explain what the following line of code does:
elements.remove('lithium')
1.  Write a line of code that would delete ‘boron’ from the list.
2.  Edit the code to determine what happens if the same element appears in the list twice and use the remove()function to remove the word.  Does it remove both instances of the word
3. Write a line of code that attempts to remove the element: neon.  What happens when the code is executed

8.  Enter and execute the following code:

 Python Program 11.8 elements = ['hydrogen', 'helicopter','lithium','beryllium','boron','carbon','element7','lithium'] print(elements) elements[1]='helium' print(elements) 
1.  Explain what the following line of code does:
elements[1]='helium'
1.  Write the line of code that would replace ‘element7’ with ‘nitrogen’ in the above list.
2. Explain what the following line of code does:
elements[-3]= 'new_element'
1.   Explain what happens when the following line of code is added to the program:                                          elements[12]= 'magnesium'

9.  Enter and execute the following code:

 Python Program 11.9 ballot = ['y', 'y', 'n', 'y', 'y', 'n', 'y', 'n', 'y','n','y','n','n','y','y','y','n'] numY = ballot.count('y') numN = ballot.count('n') print ("Number of YES votes; ", numY) print("Number of NO votes: ", numN) if numY > numN: print("The ballot was approved.") else: print("The ballot was not approved.")

a.  What is the output of this program

b.  Explain the line of code:

numY = ballot.count(‘y’)

What does the count() function do? How many arguments does it have?

c.  Change the following line of code in the program:

numY = ballot.count('Y')

How does this change the output? Is the count function case sensitive?

d.  Add the following lines of code to the program:

print(ballot.index('y'))
print(ballot.index('n'))
print(ballot.index('x'))

-- What do the new lines lines print?

-- What does the index() function do?

-- What happens if you replace ‘y’ or ‘n’ with ‘x’? Why does this happen?