Section 9.19 Group Work: Lists
It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see
https://cspogil.org/Home 1 .
Many interesting problems involve manipulating sequences of data. This activity should help you learn about two types of sequences in Python: lists and strings. Both lists and strings hold items in order and you can use an index to get the value at a location. The index of the first item in each is 0.
Learning Objectives
After completing this activity, Students should be able to:
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
Define the following list functions: append(), insert(), remove(), len(), index(), count()
Explain how to replace an item
Process Skill Goals
During the activity, students should make progress toward:
Writing code that prints a list
Writing code that edits a list - add, remove, and insert items
Checkpoint 9.19.2.
Edit the code below to answer the following questions.
Checkpoint 9.19.3.
Q-2: Which of the lists in the code have the most number of items?
digits
Correct! Digits has the most items in its lists.
media
Try Again! Use len() to check the length of each list
studentData
Try Again! Each list within a list counts as one item. Use len() to check the length of each list.
both b and c
Try Again! While b and c have the same number of items in their list, it is not the most. Use len() to check the length of each list
Checkpoint 9.19.4.
Checkpoint 9.19.5.
Checkpoint 9.19.6.
Construct a function that replaces a value in a list with a new specified value and returns the list with the new value.
def replaceVal(alist, val, newval):
idx = alist.index(val)
alist[index] = newval
return alist
Checkpoint 9.19.7.
Run this code to see what it prints. Feel free to edit!
Checkpoint 9.19.8.
Q-7: What is printed by the following code?
9
Repetition does not multiply the lengths of the lists. It repeats the items.
[3, 1, 2, 3, 1, 2, 3, 1, 2]
Yes, the items of the list are repeated 3 times, one after another.
[3, 3, 3, 1, 1, 1, 2, 2, 2]
Repetition does not repeat each item individually.
[27, 3, 6]
Repetition does not multiply the individual items.
Checkpoint 9.19.9.
Checkpoint 9.19.10.
Q-9: Which of the following lines could you use to add 7 to the list to become [3, 1, 2, 7,]?
areaCode.append(7)
Yes! You can add the item directly to the list using append
areaCode.append([7])
Try again! You cannot append a list even if it has one element
areaCode += 7
Try Again! You cannot concatenate a list and an integer, only two lists.
areaCode.extend([7])
Yes! You can add a list to the end of another list using extend.
areaCode.extend(7)
Try again! You cannot extend a list with a number.
Checkpoint 9.19.11.
Construct a function that returns the max value from a list. If there are no items in alist``return ``None
.
def getMax(alist):
---
if len(alist) == 0:
return None
---
curr = alist[0]
for item in alist:
---
if item > curr:
---
curr = item
---
return curr
Checkpoint 9.19.12.
Run this code to see what it prints. Feel free to edit!
Checkpoint 9.19.13.
Checkpoint 9.19.14.
Q-13: What is the index of “Detroit” in the list bigCities
?
2
Try Again! Remember that the index starts at 0 not at 1.
1
Yes, because lists start 0 based index, the solution would be index 1.
0
Try Again! Use index("Detroit") to find the index.
3
Try Again! Use index("Detroit") to find the index.
Checkpoint 9.19.15.
Construct a function that returns the average of the values entered into the list.
def average():
---
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)
Checkpoint 9.19.16.
If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="groupsub" id=lists_intro_groupsub data-size_limit=3> <div class="col-sm-6"> <select id="assignment_group" multiple class="assignment_partner_select" style="width: 100%"> </select> </div> <div id="groupsub_button" class="col-sm-6"> </div> <p>The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.</p> </div> </div>