Simple example: imagine you have a physical phone book and want to look up friends or family in it. The equivalent in Python would be:
phone_book = { "Mom": "123-456-7890",
"Dad": "123-456-7891",
"John Doe": "555-555-5555", # and so on...
}
If you're attempting to look up your dad's phone number in your physical phone book, you'd do so by navigating directly to the page you wrote it on and finding the entry. Similarly, looking it up in a Python dictionary is the same:
print(phone_book['Dad']) # 123-456-7891
Now that the real-world example is pretty clear, look at your example. Through .items()
, you're retrieving a key value pair where user
is simply a key to reference a specific value in the users
dictionary (like "Mom" or "Dad") and status
is the value mapped to that specific user
(like their phone numbers).
However, you're taking a copy of the users
dictionary so that you can iterate over the entire pairing of users
to statuses
. If you had
for user, status in users.items():
del[user]
you'd be modifying the dictionary you're attempting to iterate and would get an error. To avoid that, you're making a temporary copy of it to iterate through and remove the actual user
from users
(think "Remove Mom from your phone book").
In the second chunk, you are adding people to an active user dictionary. Think "Add Billy to phone book with phone number of "111-222-3344", but in this case, you're adding the user
and their corresponding status
.
TLDR: A dictionary is just a way to look things up, but in order to look something up, you need to know their identifier (name
in phone book, user
in user dictionary). If you want to do something with that identifier's value (number
in phone book, status
in user dictionary), you'd want to store it temporarily until you're done with it.