A dictionary is a data type. In a way, it works similar to how an array would work, but it works a bit differently; they utilize keys and values rather than utilizing an index. To access a value in a dictionary, you use a key rather than index.
Example:
# phone book
phonebook["Adam"] = 3108729066
phonebook["Mina"] = 9728891423
phonebook["Jasmine"] = 9491552251
phonebook["Zach"] = 4109246570
phonebook["Kendall"] = 6208291502
print(phonebook)
You can also set it up like this (I prefer this way):
# phone book
phonebook = {
"Adam" : 3108729066,
"Mina" : 9728891423,
"Jasmine" : 9491552251,
"Zach" : 4109246570,
"Kendall" : 6208291502
}
print(phonebook)
How about iterating over a dictionary
You can iterate dictionary over, similar to how you do with a list. Keep in mind, a dictionary is not like a list: a dictionary does not maintain the sequence of the values that are stored within it. In order to iterate over key value pairs, you can do this:
# phone book
phonebook = {
"Adam" : 3108729066,
"Mina" : 9728891423,
"Jasmine" : 9491552251,
"Zach" : 4109246570,
"Kendall" : 6208291502
}
for name, number in phonebook.items():
print("The phone number of %s is %d." % (name, number))
How do I remove a value?
In order to remove a specific index value, you can do this:
# phone book
phonebook = {
"Adam" : 3108729066,
"Mina" : 9728891423,
"Jasmine" : 9491552251,
"Zach" : 4109246570,
"Kendall" : 6208291502
}
del phonebook["Zach"] # sorry, Zach
print(phonebook)
You can also do this:
# phone book
phonebook = {
"Adam" : 3108729066,
"Mina" : 9728891423,
"Jasmine" : 9491552251,
"Zach" : 4109246570,
"Kendall" : 6208291502
}
phonebook.pop["Mina"] # sorry, Mina
print(phonebook)
Exercise
Let’s try to play with the phonebook concept. For this exercise, we can try to add a person with a phone number (you can pick the name and number). We also want to remove a person from the phonebook as well (again, you can pick as well). Please take a look at the solution if you get stuck!
Exercise:
# fill in your code here
phonebook = {
"Adam" : 3108729066,
"Mina" : 9728891423,
"Jasmine" : 9491552251,
"Zach" : 4109246570,
"Kendall" : 6208291502
}
# testing code (adjust this based on who you select)
if "Jim" in phonebook and "Kendall" not in phonebook:
print("Jake is listed in the phonebook.")
print("Jill is no longer listed in the phonebook.")
Solution:
# fill in your code here
phonebook = {
"Adam" : 3108729066,
"Mina" : 9728891423,
"Jasmine" : 9491552251,
"Zach" : 4109246570,
"Kendall" : 6208291502
}
phonebook["Jim"] = 9493102021
del phonebook["Adam"]
# testing code (adjust this based on who you select)
if "Jake" in phonebook and "Jill" not in phonebook:
print("Jake is listed in the phonebook.")
print("Jill is no longer listed in the phonebook.")
Output:
<script.py> output:
Jim is listed in the phonebook.
Kendall is no longer listed in the phonebook.
For more information
If you seek to learn more about Python, please visit Python’s official documentation.
Michael is an Information Technology consultant, with a focus on cybersecurity. Every day, Michael strives to learn something new, with an aim to share it with you all!