Mapping Data with Dictionaries and Sets

Beyond Ordered Lists

Overview

While lists are great for sequences, real-world data often needs labels. Dictionaries map labels to information, while Sets handle unique collections without duplicates.

Welcome to your exploration of advanced data structures. While lists keep things in order, sometimes we need to label our data or ensure every item is one-of-a-kind. Today, we'll master Dictionaries and Sets.

Dictionaries: The Key-Value Powerhouse

Key-Value Pairs

A dictionary stores data in key-value pairs. Think of a real dictionary: you look up a word (the key) to find its definition (the value).

Think of a dictionary as a set of labeled drawers. The label on the outside is the 'Key', and the item inside is the 'Value'. Keys must be unique—you wouldn't want two drawers with the same name! But the values inside can be anything from numbers to entire lists.

Safe Data Retrieval

Using dict['key'] can crash your program if the key is missing. Instead, use .get() for safe access.

Let's try to access some data. We have a dictionary of grades. Try clicking the 'Get Grade' button for a student who isn't there. See that? By using dot-get, the program didn't crash. It simply returned a default value of zero because 'Alice' wasn't in our dictionary yet. Oh no! Accessing a missing key directly causes a KeyError. This is why dot-get is your best friend.

Sets: The Art of Uniqueness

Unique Collections

A Set is an unordered collection where every element must be unique. Python automatically ignores duplicates.

Now, let's look at Sets. Imagine a bag where you throw in colors. You add 'red'. You add 'blue'. But when you try to add 'red' again, the bag stays the same. Sets only care about unique items.

Set Operations in Action

Sets allow for mathematical operations like Intersection (&) and Union (|).

Sets are powerful for comparing groups. Here we have Group A and Group B. Click 'Intersection' to see who is in both. The union uses the pipe symbol. It merges everything together, but remember: even in a union, duplicates are removed! The intersection uses the ampersand symbol. It finds the items that exist in both sets—in this case, just Bob.

The Empty Set Trap

Common Pitfalls

Watch out for the empty set trap! If you type empty curly braces, Python assumes you want a dictionary. To create a truly empty set, you must use the set function. Also, because sets have no order, trying to access an index will result in an error.

Classroom Manager Role-Play

Help the coding club instructor organize student data using Dictionaries and Sets.

Meet Mr. Python, the coding club instructor. He needs help managing his students. Talk to him to decide which data structure to use for different tasks.

Diagnosis: The Duplicate Bug

A developer is trying to keep a list of unique usernames, but duplicates are still appearing. Write a 1-2 sentence solution using what you learned about sets.

Look at this code snippet. Why is the username list failing to stay unique? Type your diagnosis and fix below.