Practice Building with Python Collections

Put your Python collections knowledge into practice. Build a task tracker, a grade calculator, and a shopping cart using lists, dictionaries, sets, and tuples.

5 min read

Reading about collections teaches you what each type does. Practice building with Python collections teaches you when to use each one and how they fit together. This article provides three hands-on projects that use the collection types and methods covered throughout this section. Each project starts with a clear goal, walks through the collection choices involved, and gives you a working program that you can extend on your own.

The projects are designed to be completed in order. The task tracker focuses on lists. The grade calculator adds dictionaries and nested collections. The shopping cart combines all four collection types into a single program. If you get stuck deciding which collection fits a step, revisit the decision guide in choosing the right Python collection type. By the end, you will have written code that uses every major collection feature introduced in this section, from list methods to dictionary views to set operations.

Project 1: Task tracker

A task tracker is the simplest useful program that exercises lists. You need to store tasks in order, add new ones, mark them complete by removing them, and display the current list. A list is the natural choice because tasks have a meaningful order and the list of tasks changes as work progresses.

pythonpython
tasks = []
tasks.append("Write report")
tasks.append("Review pull request")
tasks.append("Update documentation")
print("Tasks:", tasks)
done = tasks.pop(0)
print("Completed:", done)
print("Remaining:", tasks)

Start with an empty list. Add tasks as strings. When a task is done, remove it from the front of the list to process tasks in first-in-first-out order, or from the end for last-in-first-out. Display the remaining list after each change to see the state of your work.

To extend this project, add a priority system by storing each task as a tuple of priority and description. Sort the list by priority before displaying. Add a command loop that asks the user for input and performs the corresponding action. Each extension adds a new collection skill: tuples for compound data, sorting for ordered display, and a loop for interactive control.

Project 2: Grade calculator

A grade calculator uses a dictionary to map student names to their scores. Each student's scores are stored in a list, making this a dictionary of lists, the most common nested collection pattern. The program computes averages, finds the top performer, and displays a summary.

pythonpython
grades = {}
grades["Alice"] = [92, 88, 95]
grades["Bob"] = [85, 90, 87]
grades["Charlie"] = [78, 82, 80]
 
for name, scores in grades.items():
    avg = sum(scores) / len(scores)
    print(f"{name}: {avg:.1f}")

Iterating over the items gives you both the student name and their score list in each iteration. Computing the average uses sum and len on the inner list. Finding the top student uses the max function with a key that computes the average, combining the built-in functions covered in Python's built-in collection functions with dictionary iteration.

To extend this project, add letter grade thresholds using conditional logic. Store the final results in a new dictionary mapping names to letter grades. Sort the display by average score descending. Each extension builds on the previous one while introducing a new technique.

Project 3: Shopping cart

A shopping cart combines multiple collection types in a single realistic program. The product catalog is a dictionary mapping product IDs to details like name and price. The cart itself is a list of product IDs, preserving the order items were added. A set tracks the unique product categories in the cart, demonstrating how sets complement lists when you need both order and uniqueness.

pythonpython
catalog = {
    "p1": {"name": "Notebook", "price": 12.99, "category": "office"},
    "p2": {"name": "Headphones", "price": 45.00, "category": "electronics"},
}
cart = ["p1", "p2", "p1"]
categories = {catalog[pid]["category"] for pid in cart}
total = sum(catalog[pid]["price"] for pid in cart)

The catalog is a nested dictionary, the cart is a list that allows duplicates, the categories set uses a set comprehension to extract unique values, and the total uses sum with a generator expression. Four collection types, three different patterns, one program. This is what real Python code looks like, and it is built entirely from the concepts covered in this section.

To extend this project, add a cart display that groups items by name and shows quantities using a dictionary or Counter. Add remove and clear operations. Add a checkout function that validates the cart against inventory levels stored alongside the catalog. Each extension is a real-world feature, and each one maps naturally to a collection operation you have already learned.

Rune AI

Rune AI

Key Insights

  • Build a task tracker with a list to practice append, remove, pop, and for-loop iteration.
  • Build a grade calculator with a dictionary of lists to practice nested access, sum, and max with a key.
  • Build a shopping cart combining dicts, lists, and sets to see how collection types work together.
  • Each project reinforces the decision-making skill of choosing the right collection for the task.
RunePowered by Rune AI

Frequently Asked Questions

What is a good first project to practice Python lists?

Build a simple task tracker. Use a list to hold task strings, add tasks with append, mark them complete by removing them with pop or remove, and display the list with a for loop. This exercises list creation, modification, and iteration in a single small program that you can write in under twenty lines.

How can I practice using dictionaries?

Build a grade calculator. Use a dictionary where student names are keys and lists of scores are values. Add scores by appending to the list, compute averages by summing and dividing, and find the top student using max with a key function. This exercises dictionary access, nested list values, and built-in functions together.

How do I combine multiple collection types in one program?

Build a shopping cart. Use a dictionary to map product IDs to details, a list to hold the items in the cart in order, and a set to track which unique product categories the cart contains. This small program uses all four collection types and shows how they complement each other in a realistic scenario.

Conclusion

The best way to internalize Python collections is to build things with them. Start with the task tracker for lists, move to the grade calculator for dictionaries, and then combine everything in the shopping cart. Each project uses the methods and patterns from the articles in this section in a context where the purpose of each collection type is clear.