Skip to main content

Practice 5 — Lists

Skills: 4​

Reading: the Notes sections of Class 13, Class 14, and Class 21​

Working with Lists​

Here's some cafe sales data:

cafe_data = table(
["day", "drinks-sold"],
[
["Mon", 45],
["Tue", 30],
["Wed", 55],
["Thu", 40],
["Fri", 60],
],
)

Extracting Lists from Tables​

Extract the drinks-sold column as a list using .column. What does this list look like? How is it different from the original table?

Mathematical Operations on Lists​

max, min, and sum are built in, but you'll need to import the statistics library:

import statistics as S

Using the extracted list, find:

  • The maximum number of drinks sold (use max)
  • The minimum number of drinks sold (use min)
  • The average daily sales (use S.mean)
  • The total weekly sales (use sum)

Creating List Literals​

Create these lists directly (without using a table):

  • A list of test scores: 85, 92, 78, 96, 88, 91
  • An empty list
  • A list of names: "Alice", "Bob", "Carol"

Using your test scores list, calculate:

  • The highest score (use max)
  • The average score (use S.mean)
  • The total of all scores (use sum)

Transforming Lists with Map​

map is built in (remember to wrap its result with list(...)).

Convert these Celsius temperatures to Fahrenheit: 0, 10, 20, 30, 40

The formula is: (celsius × 9/5) + 32

Use map with a function that applies this formula to each temperature.

Working with Student Grades​

Given these raw scores: 78, 85, 92, 67, 88, 94, 73, 81, 90, 86

Apply a 5-point curve to all scores using map with a lambda function that adds 5 to each score.

Then convert the curved scores to letter grades using map with a helper function that checks the score ranges (since it needs an if, it can't be a lambda):

  • A: 90 and above
  • B: 80-89
  • C: 70-79
  • D: 60-69
  • F: below 60

How many students got each letter grade?

Filtering Lists​

Use filter with a lambda function to find all the passing scores (60 and above) from your curved scores list. How many students passed?

Wrap-Up​

When would you use a list instead of a table? What real-world data could you analyze using these list operations?