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

Q-8: 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.

Note

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

Run this code to see what it prints.

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

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

Note

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

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.