5.29. Group Work: Functions and 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.
Learning Objectives
Students will know and be able to do the following.
Content Objectives:
Use positive and negative indices to access elements of a list.
Identify the purpose of common list methods and common methods that take lists as parameters
Use the slice operator to copy parts of a list.
Process Objectives:
Predict the output of code with lists (Information Processing)
Write code using the slice operator (Assessment)
5.29.1. List Indexing¶
A list holds items in order and you can get the value at an index, just like you can with strings.
Run this code to see what it prints.
Run this code to see what it prints.
- hi
- This would be true if it was returning the item at index 0 or -4.
- 3
- This would be true if it was returning the item at index 1 or -3.
- buy
- This is returning the second to the last item, the one at index -2.
- 4
- This would be true if it was returning the item at index 3 or -1.
- Nothing, there will be an error.
- This code will run without any errors.
Q-6: What will the following code print?
1 def list_get(lst):
2 return lst[-2]
3 l = ["hi", 3, 'buy', 4]
4 print(list_get(l))
Q-7: Describe in your own words how negative indices work.
5.29.2. Built-in Functions That Work on Lists¶
There are several built-in functions in Python that work on lists.
Run this code to see what it prints.
Write a function avg_with_drop
that takes a list, num_list
and returns the average of the values in the list, but it does not include the highest or lowest value in the average. For example, avg_with_drop([1,2,3,4])
should return 2.5
.
5.29.3. List Methods¶
Lists are objects of the list
class and have methods that operate on list objects.
Run this code to see what it prints.
- [2, 5, 7, 3]
- This is what the list looks like before the pop executes.
- [5, 7, 3]
- This would be true if pop removed the first value that was passed in, but it takes an index and removes the item at that index.
- [2, 7, 3]
- This would be true if pop removed the item at index 1, but it removes the item at index 2 and the first item is at index 0.
- [2, 5, 7]
- This would be true if pop removed the last item, but it removes the one at index 2.
- [2, 5, 3]
- Correct. This adds 3 at the end and then removes the item at index 2.
Q-12: What would the following code print?
def list_trans(lst):
lst.append(3)
lst.pop(2)
return lst
l1 = [2, 5, 7]
print(list_trans(l1))
Note
Lists are mutable (changeable). List methods like append and pop change the current list.
Run this code to see what it prints.
- None
- It prints the return value from the reverse method which is None.
- [2, 5, 7]
- This would be true if it printed the value of
- [7, 5, 2]]
- This would be true if pop removed the item at index 1, but it removes the item at index 2 and the first item is at index 0.
- Nothing, there will be an error.
- This would be true if pop removed the last item, but it removes the one at index 2.
Q-14: What is the last thing the following code prints?
def list_trans(lst):
r = lst.reverse()
print(lst)
print(r)
l1 = [2, 5, 7]
list_trans(l1)
-
Q-15: Drag each built-in function name to what it does.
Read the chapter on functions and try again.
- pop(index)
- Removes the value at the specified index.
- append(item)
- Adds the items (value or list) to the end of the current list.
- extend(list)
- Adds all the contents of the passed list to the end of the current list.
- sort()
- Sort the contents of the list in ascending order.
- reverse()
- Reverse the contents of the list.
5.29.4. Using the Slice Operator¶
You can use the slice operator[n:m] with lists to get a new list just like you can with strings.
Run this code to see what it prints.
Note
The slice operator always returns a new object. It doesn’t change the current object (list or string).
- [2, 3, 4, 5]
- It returns items starting from the 3rd from the right and ending before the last.
- [2, 3, 4]
- It returns items starting from the 3rd from the right and ending before the last.
- [3, 4, 5]
- It returns items starting from the 3rd from the right and ending before the last.
- [3, 4]
- It returns items starting from the 3rd from the right and ending before the last.
- Nothing, there will be an error.
- The code will run withtout an error.
Q-18: What does the following code print?
alist = [1, 2, 3, 4, 5]
l2 = alist[-3: -1]
print(l2)
Write a function first_half
that takes a list and returns a new list (use the slice operator) with just the items from the first half of the original list. For example, first_half([1,2,3,4]) would return [1, 2] and first_half([7,8,9]) should return [7].
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.