Skip to main content

Section 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 1 .

Note 5.29.1.

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)

Subsection 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.

Checkpoint 5.29.2.

Q-1: What is the last thing that will be printed when the code below runs?

Checkpoint 5.29.3.

Run this code to see what it prints.

Checkpoint 5.29.4.

Run this code to see what it prints.

Checkpoint 5.29.5.

Q-4: What characters are used to indicate the start and end of a list?

Checkpoint 5.29.6.

Q-5: What index is used to get the first item in a list?

Checkpoint 5.29.7.

Q-6: What negative index is used to get the last item in a list?

Checkpoint 5.29.8.

    Q-7: What will the following code print?
    def list_get(lst):
        return lst[-2]
    l = ["hi", 3, 'buy', 4]
    print(list_get(l))
    
  • 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.

Checkpoint 5.29.9.

Q-8: Describe in your own words how negative indices work.

Subsection 5.29.2 Built-in Functions That Work on Lists

There are several built-in functions in Python that work on lists.

Checkpoint 5.29.10.

Run this code to see what it prints.

Checkpoint 5.29.11.

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.

Checkpoint 5.29.12.

Subsection 5.29.3 List Methods

Lists are objects of the list class and have methods that operate on list objects.

Checkpoint 5.29.13.

Run this code to see what it prints.

Checkpoint 5.29.14.

Q-13: What class (type) is a list?

Checkpoint 5.29.15.

    Q-14: 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))
    
  • [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.

Note 5.29.16.

Lists are mutable (changeable). List methods like append and pop change the current list.

Checkpoint 5.29.17.

Run this code to see what it prints.

Checkpoint 5.29.18.

    Q-16: 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)
    
  • 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.

Checkpoint 5.29.19.

Subsection 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.

Checkpoint 5.29.20.

Run this code to see what it prints.

Checkpoint 5.29.21.

Q-19: In [:2] what is the start index?

Checkpoint 5.29.22.

Q-20: In [2:] what is the end index?

Note 5.29.23.

The slice operator always returns a new object. It doesn't change the current object (list or string).

Checkpoint 5.29.24.

    Q-21: What does the following code print?
    alist = [1, 2, 3, 4, 5]
    l2 = alist[-3: -1]
    print(l2)
    
  • [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.

Checkpoint 5.29.25.

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.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="groupsub" id=func_list_groupsub data-size_limit=4> <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>
https://cspogil.org/Home