Membership and View Methods
Now that we can safely retrieve data, we need ways to inspect the dictionary as a whole—checking if keys exist and pulling out lists of all labels or data.
The in Operator (Membership Testing)
The in operator is the fastest and most readable way to check if a key exists in a dictionary before you try to use it. It returns a Boolean value: True or False. The "in" operator only checks keys, not values.
- Use the print function with the in operator to see if color is in new_dict.
- Use the print function with the in operator to see if chicken is in new_dict.
- Use the print function with the in operator to see if red is in new_dict.
Viewing Dictionary Data
Sometimes you need to look at the dictionary's contents without targeting a specific key. Python provides three "view" methods for this:
- .keys()This method returns a view of all the keys in the dictionary. It is useful when you need a list of all the labels or identifiers available.
- Use the .keys method to see the keys.
- .values()This method returns a view of all the values (the data) stored in the dictionary, ignoring the keys entirely.
- Use the .values method to see the values.
- .items()This is one of the most powerful methods. It returns each key-value pair as a tuple. This is the standard way to look at everything inside the dictionary at once.
- Use the .items method to see the items.
Why use these?
These "view" objects are dynamic. If the dictionary changes, the view updates automatically. They are most commonly used in loops (which we will cover soon) to iterate through data.
A Note on .items() and Tuples
When you use the .items() method, Python gives you back the dictionary's data in pairs. If you look closely at the output, you’ll notice these pairs are wrapped in parentheses () rather than square brackets [].
These small groups inside the parentheses are called Tuples.
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named more_methods.py in your part31 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: Membership Check
Use the in operator to verify if a specific key is
present.
-
Task: Create
fruit_stock = {"apples": 10, "bananas": 5}. -
Goal: Print the result of checking if
"pears"is infruit_stock.
Exercise 2: Documentation Discovery
Part of being a coder is learning to read documentation.
-
Task: Go to the
Python list methods. Find a method we haven't covered yet (like
.clear()or.copy()). - Goal: Create a dictionary and try that new method on it. Print the dictionary before and after to see what changed.
Exercise 3: Data Dump
Extract just the values from a dictionary.
-
Task: Create
scores = {"Math": 90, "Science": 85}. -
Goal: Use the
.values()method to print only the grades, without the subject names.
Exercise 4: Pairing Up
View the full relationship between keys and values and notice the tuples.
-
Task: Create
user = {"id": 123, "name": "Sky"}. -
Goal: Use
.items()to print the dictionary. Observe how the pairs are held inside parentheses().
Exercise 5: Number Keys and Membership
Confirm that in works with integer keys as well.
-
Task: Create
codes = {101: "Active", 404: "Error"}. -
Goal: Check if the integer
101is incodesand print the result.
Don't Forget to commit and Push!