Learn Without Walls
← Back to Python Practice Labs
Lab 5 of 10

Lists & Dictionaries

Python’s most powerful built-in data structures

← Lab 4: Functions Lab 5 of 10 Lab 6: Strings →
⏳ Loading Python... (first load takes ~10 seconds)

📖 Concept Recap

👀 Worked Example

A shopping cart combining lists and dicts:

cart = [] products = { "apple": 1.20, "bread": 2.50, "milk": 3.00, "cheese": 4.75 } cart.append("apple") cart.append("bread") cart.append("milk") cart.append("apple") # duplicate! # Count items item_counts = {} for item in cart: item_counts[item] = item_counts.get(item, 0) + 1 print("Cart:", item_counts) # Calculate total total = sum(products[item] * count for item, count in item_counts.items()) print(f"Total: ${total:.2f}") # List comprehension — items over $2 pricey = [item for item, price in products.items() if price > 2] print("Items over $2:", pricey)
✏️ Guided

Exercise 1 — Student Roster Manager

Complete the roster manager by filling in the blanks.

Output will appear here...
💡 Hint: Use .append() to add to a list. reverse=True sorts highest-first. len(grades) for the count in the average calculation.
💪 Independent

Exercise 2 — Word Frequency Analyzer

Given the word list below, write code to find:

  1. The unique words (no duplicates)
  2. Word frequency (how many times each word appears)
  3. The most common word
  4. Words with more than 4 letters
Output will appear here...
💡 Hint: set(words) for unique words. Build frequency with a loop or {} and .get(). max(freq, key=lambda w: freq[w]) finds the most common. Use a list comprehension with len(w) > 4.
🔥 Challenge

Exercise 3 — Group by Grade

Write a function group_by_grade(grade_dict) that takes a dictionary of name: score pairs and returns a new dictionary grouping names by letter grade (A/B/C/D/F). Test with at least 8 students.

Expected output structure: {"A": ["Alice", "Carol"], "B": ["Bob"], ...}

Output will appear here...
💡 Hint: Start with an empty dict for each grade. Loop through students, determine their letter grade, then append to the right list. Use result.setdefault("A", []).append(name) to avoid KeyErrors.
🏆 Mini Project

Mini Project — Shopping Cart System

Build a complete shopping cart. Start with an inventory of 5 products (name, price, stock). Write these 4 functions:

Then simulate: add 3 items (one twice), remove 1, print the receipt.

Output will appear here...
💡 Hint: The cart dict stores {"apple": 3}. In add_to_cart, check if quantity <= inventory[product]["stock"]. The receipt should show each item, quantity, unit price, line total, and a grand total.

✅ Lab 5 Complete!

You’ve mastered list comprehensions, dict operations, sorting with key functions, and built a complete data-driven shopping system. These skills are the foundation of data analysis.

Continue to Lab 6: String Methods →

← Lab 4: Functions Lab 5 of 10 Lab 6: Strings →