Python / Lists and arrays

Lists with loops

Use a for loop with a list to find the total and average, count values that match a condition, find the largest value yourself, and build a new list with append. Baccalaureate

Total and average

Python
marks = [78, 92, 85, 64, 90]
total = 0
for mark in marks:
    total += mark
print(total)
print(total / len(marks))
Console
409
81.8
total += mark is short for total = total + mark. Start the total at 0 before the loop and print it after it, not indented, or it prints on every turn.

Counting with a condition

To count values that match a condition, use a counter that starts at 0 and goes up by 1 inside an if inside the loop:

Python
record_list = ["absent", "present", "present", "absent", "present"]
count = 0
for day in record_list:
    if day == "present":
        count = count + 1
print(count)
Console
3

If you need the position, use range(len(...)); the program then works however many values there are:

Python
body_temp_list = [36.1, 36.4, 37.2, 36.5, 37.0]
for i in range(len(body_temp_list)):
    if body_temp_list[i] >= 37.0:
        print("Day " + str(i + 1) + ": Caution!")
Console
Day 3: Caution!
Day 5: Caution!

Finding the largest value yourself

The max() function does this, but the idea behind it matters: keep the "best value so far" and compare each value with it; when you find a bigger one, replace it:

Python
marks = [78, 92, 85, 64, 90]
biggest = marks[0]
for mark in marks:
    if mark > biggest:
        biggest = mark
print(biggest)
Console
92

Building a new list with append

Start with an empty list [] and add to its end with .append(value). This is how you collect the values that match a condition:

Python
marks = [78, 45, 85, 30, 90]
passed = []
for mark in marks:
    if mark >= 50:
        passed.append(mark)
print(passed)
print(len(passed))
Console
[78, 85, 90]
3
When looking for the largest or smallest value, start with the first value marks[0], not 0; if all values were negative you would get 0, which is not even in the list.

Practice

1Fill in the blankCurriculum practice

Fill in the two blanks using len(body_temp_list) instead of writing 14, so the average temperature is printed rounded to one decimal place: 36.5.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
body_temp_list = [36.1, 36.4, 36.6, 36.5, 36.7, 36.0, 36.2, 36.8, 36.6, 36.5, 36.9, 36.4, 36.7, 37.0]
total = 0
for i in range(___):
    total = total + body_temp_list[i]
print(round(___, 1))
Console
Press Run to see the result.
2Write the programCurriculum practice

Go through every value of body_temp_list and print Watch out! for each value that is 37.0 or more.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
body_temp_list = [36.1, 36.4, 36.6, 36.5, 36.7, 36.0, 36.2, 36.8, 37.1, 36.5, 36.9, 36.4, 36.7, 37.2]
Console
Press Run to see the result.
3Write the programCurriculum practice

Count the absent days: add 1 to count whenever the value is "absent", then print count after the loop.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
record_list = ["absent", "present", "present", "present", "absent", "present", "absent", "present", "present", "present", "absent", "present", "present", "absent"]
count = 0
Console
Press Run to see the result.
4Write the programCurriculum practice

Print 85 or above once for each mark in mark_list that is 85 or more.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
mark_list = [48, 55, 74, 85, 63, 57, 91]
Console
Press Run to see the result.
5Write the programExtra practice

The lowest temperatures of 6 winter nights are in temps. Find the smallest value without using min(), and print Lowest: -2.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
temps = [3, -1, 4, 0, -2, 5]
Console
Press Run to see the result.
6Write the programExtra practice

From prices, build a new list cheap holding only the prices under 100, using append, then print it (output [45, 80, 15]).

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
prices = [45, 120, 80, 300, 15]
Console
Press Run to see the result.

Key points

  • Total: total = 0 before the loop, total += x inside it; average is total / len(list).
  • Counting: a counter that starts at 0 and goes up inside an if inside the for.
  • Largest value: start with list[0] and replace it whenever you find a bigger one.
  • new = [], then new.append(x), to build a new list.