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.
Note
If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.
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
Edit the code below to answer the following questions.
- 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
Q-2: Which of the lists in the code have the most number of items?
Construct a function that replaces a value in a list with a new specified value and returns the list with the new value.
Run this code to see what it prints. Feel free to edit!
- 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.
Q-7: What is printed by the following code?
- 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.
Q-9: Which of the following lines could you use to add 7 to the list to become [3, 1, 2, 7,]?
Construct a function that returns the max value from a list. If there are no items in alist``return ``None
.
Run this code to see what it prints. Feel free to edit!
- 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.
Q-13: What is the index of “Detroit” in the list bigCities
?
Construct a function that returns the average of the values entered into the list.
-
Q-15: Drag each term to its definition
Read the chapter on lists and try again.
- count
- Returns the number of times a specified value appears in the list
- append
- Adds a value to the end of a list.
- len
- Returns the number of items in a list.
- remove
- Removes an item from a list.
- type
- Returns the class name (type) of the current object.
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.
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.