- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
Using Python Dictionaries
Dictionaries are one of the fundamental data types that we use in Python to solve real problems. These are handy when we don't need a sequential list of items, and it is more useful to have unique identifiers for looking up values. In this hands-on lab, we'll be working through some exercises demonstrating that we understand how to add, remove, modify, and read items from dictionaries in Python. To feel comfortable completing this lab you'll want to know how to do the following: * Working with dictionary literals. Watch the "Dictionaries" video from the Certified Entry-Level Python Programmer Certification course. * Using Dictionary functions and methods. Watch the "Dictionary Methods" video from the Certified Entry-Level Python Programmer Certification course.
Lab Info
Table of Contents
-
Challenge
Create the emails Dictionary and Add Initial Items
Our first few tasks require us to create the
emailsvariable that we're going to work with throughout the lab and then add some information to it. Here's how we complete the first task:using-dictionaries.py (partial)
# 1) Set the emails variable to be an empty dictionary emails = {} assert emails == {}, f"Expected `emails` to be {{}} but got: {repr(users)}"Now if we run the file (
python3.7 using-dictionaries.py), we should see the error for the second task:$ python3.7 using-dictionaries.py Traceback (most recent call last): File "using-dictionaries.py", line 12, in <module> }, f"Expected `emails` to be {{'ashley': '[email protected]', 'craig': '[email protected]', 'elizabeth': '[email protected]'}} but got: {repr(emails)}" AssertionError: Expected `emails` to be {'ashley': '[email protected]', 'craig': '[email protected]', 'elizabeth': '[email protected]'} but got: {}This error shows us that we need to add values to the dictionary before we can continue. The task also specifies that we shouldn't just reassign the
emailsvariable. Here's an example solution to this:using-dictionaries.py (partial)
# 2) Add 'ashley', 'craig', and 'elizabeth' to the emails dictionary without reassigning the variable. emails['ashley'] = '[email protected]' emails['craig'] = '[email protected]' emails['elizabeth'] = '[email protected]' assert emails == { 'ashley': '[email protected]', 'craig': '[email protected]', 'elizabeth': '[email protected]' }, f"Expected `emails` to be {{'ashley': '[email protected]', 'craig': '[email protected]', 'elizabeth': '[email protected]'}} but got: {repr(emails)}" -
Challenge
Remove craig and Add dalton
For tasks 3 and 4, we need to remove the
craigkey/value pair and add one calleddalton. Here's an example solution for getting rid ofcraig:using-dictionaries.py (partial)
# 3) Remove 'craig' from the emails dictionary without reassigning the variable. del emails["craig"] assert emails == { "ashley": "[email protected]", "elizabeth": "[email protected]", }, f"Expected `emails` to be {{'ashley': '[email protected]', 'elizabeth': '[email protected]'}} but got: {repr(emails)}"When we run the script again, we'll get an error about it expecting a
dalton. To fix that, we need to add it in. Here's how:# 4) Add 'dalton' to the emails dictionary without reassigning the variable. emails["dalton"] = "[email protected]" assert emails == { "ashley": "[email protected]", "elizabeth": "[email protected]", "dalton": "[email protected]", }, f"Expected `emails` to be {{'ashley': '[email protected]', 'elizabeth': '[email protected]', 'dalton': '[email protected]'}} but got: {repr(emails)}"The
delstatement will allow us to remove an item with the specified key to complete task three. For task four, we've just added add another key/value pair. -
Challenge
Return a List of Keys and a List of Values from the emails Dictionary
For tasks five and six we'll be extracting information from the
emailsdictionary to populate new lists forusersandemail_list. Theuserslist will contain all of the keys fromemailsand theemail_listwill include all of the values.using-dictionaries.py (partial)
# 5) Return a list of keys from the emails dictionary as `users` users = list(emails.keys()) assert users == [ "ashley", "elizabeth", "dalton", ], f"Expected `users` to be ['ashley', 'elizabeth', 'dalton'] but got: {repr(users)}" # 6) Return a list of values from the emails dictionary as `email_list` email_list = list(emails.values()) assert email_list == [ "[email protected]", "[email protected]", "[email protected]", ], f"Expected `email_list` to be ['[email protected]', '[email protected]', '[email protected]'] but got: {repr(email_list)}" -
Challenge
Return a List of Tuples Called pairs Representing the Key/Value Pairs in emails
For the final task, we'll extract a new list called
pairsfromemailsthat will include a 2-tuple for each key/value pair in theemailsdictionary.using-dictionaries.py (partial)
# 7) Return a list of tuples called `pairs` representing the key/value pairs in `emails`. pairs = list(emails.items()) assert pairs == [ ("ashley", "[email protected]"), ("elizabeth", "[email protected]"), ("dalton", "[email protected]"), ], f"Expected `pairs` to be [('ashley', '[email protected]'), ('elizabeth', '[email protected]'), ('dalton', '[email protected]')] but got: {repr(pairs)}"
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.