08 Lists as Data¶
A list stores multiple related values in one place, such as test scores, system readings, or login attempts.
Run in Binder Open in Colab Download notebook
Read here. Practice in a notebook.
This page shows the original notebook. Open Binder or Colab to run and edit cells. Work one cell at a time: some examples are deliberately broken for debugging practice. Download your work before leaving Binder; its sessions are temporary.
Learning Objectives¶
By the end, you should be able to:
- Create a list of related data.
- Access items using an index that starts at
0. - Use a loop to process each item in a list.
- Calculate a total and average from list data.
Concept Introduction (no code first)¶
A list is a collection of related data. Instead of storing one value at a time, we can store many values together in a list.
Creating a List¶
A list uses square brackets. Items are separated by commas. len() tells us how many items are in the list.
Think Before You Run: How many scores are in this list?
# A list of related score values
scores = [88, 91, 85, 90]
# len() returns the number of items in the list.
print("Scores:", scores)
print("Number of scores:", len(scores))
Accessing Elements¶
Each item has a position number called an index. List positions start at 0, so the first item is at position 0.
Think Before You Run: What value will scores[0] return?
# List positions start at 0.
scores = [70, 80, 90]
print("First score:", scores[0])
print("Second score:", scores[1])
print("Third score:", scores[2])
Think Before You Run¶
What happens if we use an index that does not exist?
# Intentional error: there is no item at index 3.
# Run this once, read the message, then move on.
system_readings = [72, 75, 81]
print(system_readings[3])
Looping Through a List¶
A loop can go through each item in a list one by one. This is helpful when we want to print, check, or calculate with every value.
# The loop handles one login attempt value at a time.
login_attempts = [1, 3, 2, 5]
for attempts in login_attempts:
print("Login attempts:", attempts)
Loop Visualization¶
A loop moves through the list one item at a time:
scores = [70, 80, 90]
score = 70 -> process
score = 80 -> process
score = 90 -> process
Loop ends
# Watch the loop process each disk reading.
disk_readings = [72, 75, 81]
for reading in disk_readings:
print("Checking reading:", reading)
Data Processing (sum and average)¶
We are processing the data in the list. sum() adds the values, and len() counts how many items are in the list.
Think Before You Run: What total do you expect for these three readings?
# Compute total and average.
readings = [70, 80, 90]
total = sum(readings)
average = total / len(readings)
print("Total:", total)
print("Average:", average)
Real-World Examples¶
Lists are useful for small IT data tasks, such as system logs, usage metrics, login attempts, and monitoring values.
# Disk usage monitoring
disk_usage_values = [68, 74, 82, 91]
threshold = 80
for usage in disk_usage_values:
print("Disk usage:", usage)
if usage > threshold:
print("Warning: above threshold")
# Login attempt data
login_attempts = [1, 2, 1, 4]
average_attempts = sum(login_attempts) / len(login_attempts)
print("Average login attempts:", average_attempts)
Common Mistakes¶
Common list mistakes include using an index that does not exist, forgetting the loop variable, or forgetting to start a total at 0. Always check your list size and loop structure.
Index out of range:
# Broken: this list has positions 0, 1, and 2 only.
values = [10, 20, 30]
print(values[3])
Forgetting the loop variable:
# Fixed pattern
for value in values:
print(value)
Missing initialization:
# Start the total before the loop.
total = 0
Guided Practice¶
Change the list values and rerun the cells. Keep the same structure.
# TODO: Change the scores and rerun.
scores = [78, 84, 90]
total_score = sum(scores)
average_score = total_score / len(scores)
print("Total score:", total_score)
print("Average score:", average_score)
# TODO: Change the threshold or readings.
system_readings = [62, 77, 88]
threshold = 80
for reading in system_readings:
if reading > threshold:
print(reading, "is above the threshold")
else:
print(reading, "is okay")
Parsons Problem¶
The lines below are scrambled. Put them in the correct order in the next cell.
average = total / len(readings)
print("Average:", average)
readings = [70, 80, 90]
total = sum(readings)
# TODO: Reorder the scrambled lines from above.
# Create the list, calculate total, calculate average, then print.
readings = [70, 80, 90]
total = sum(readings)
average = total / len(readings)
print("Average:", average)
Mini Lab¶
Create a list, process the data with a loop, and print the total and average.
# Mini Lab: system readings
# Step 1: Create a list of readings.
# Step 2: Start total at 0.
# Step 3: Use a loop to add each reading.
# Step 4: Calculate the average.
# TODO: Change the values to your own sample data.
system_readings = [71, 76, 83, 79]
total = 0
for reading in system_readings:
total = total + reading
average = total / len(system_readings)
print("System readings:", system_readings)
print("Total:", total)
print("Average:", average)
Wrap-Up¶
Lists store related data in one place. Loops process data one item at a time.
Next, you will organize your code using functions.
Reflection¶
What kind of real-world data could you store in a list for your field or daily work?