Collections
Project: study tracker
Combine a list of records, dictionary lookup, a set, and a comprehension.
Lesson 23 of 63
Reading: about 2 min
Lesson + practice: about 17 min
Difficulty: Beginner
On this page4 sections
Create study_tracker.py. Store each session as a dictionary inside a list. Then calculate the total, find unique subjects, and select longer sessions.
Build the tracker
sessions = [
{"subject": "Python", "minutes": 25},
{"subject": "SQL", "minutes": 40},
{"subject": "Python", "minutes": 30},
]
total = 0
subjects = set()
for session in sessions:
total += session["minutes"]
subjects.add(session["subject"])
long_sessions = [session for session in sessions if session["minutes"] >= 30]
print(f"Total: {total} min")
print(f"Unique subjects: {len(subjects)}")
print(f"Long sessions: {len(long_sessions)}")Total: 95 min Unique subjects: 2 Long sessions: 2
Why this works
The list keeps all session records, including repeated subjects. The loop adds each record’s minutes and stores each subject in a set, which removes duplicates. The comprehension creates a separate list of records that meet the longer-session threshold; it leaves the original sessions list unchanged.
Check and extend
Add another 20-minute SQL session. Predict the new total and counts before rerunning: 115 minutes, two unique subjects, and two long sessions.
Why use a set for subjects?
Reveal answer
A set keeps each subject only once, so its length counts unique subjects.
Try another case
After adding a 35-minute Excel session, what are the total, unique-subject count, and long-session count?
Check your answer
Reveal answer
The total is 130 minutes, there are three unique subjects, and there are three sessions of at least 30 minutes.
Your progress
Reveal the check answer or reach the end to complete this lesson.