Reversing a dictionary, trouble understanding

HI everyone, I am trying to understand the practice problem in dictionaries, which is number 8.

Screen Link:
https://app.dataquest.io/m/1011/python-fundamentals-practice-problems/8/reversing-a-dictionary

The dictionary is entitled name_to_email. You input a name, and it returns the email address of that person. This was fairly straightforward to create after extracting it from a csv file. Reversing the dictionary was difficult.

I couldn’t understand the logic, and so I haven’t written any code to it but the answer is as follows:

email_to_name ={}
for name in dict(name_to_email):
email = (name_to_email[name])
email_to_name[email] = name

name = email_to_name[‘[email protected]’]
print(name)

Can someone explain to me what happens when you make a for loop through a dictionary. For some reason when its hands on I’m able to deal with dictionaries, but usually, I have trouble with them because the syntax, while I understand, just confuses me to no end. I guess I’m going to get used to it by practice.

Hi @MohammedElnaiem

When you search the dictionary using for you go through all the keys in the dictionary.

What happens in this case.

  1. You have created an email_to_name dictionary - which should be the reverse dictionary name_to_email. And it means that the value and the key are changed in places.
  2. You consistently call the keys from the dictionary name_to_email. In this dictionary, name are keys.
  3. you get a value from the dictionary name_to_email using the current key. This value is assigned to the e-mail variable
  4. You create an email key in the dictionary email_to_name and you assign a name as a value. Getting the reverse pair for the dictionary name_to_email.

After you have done all the above, you get 2 dictionaries. One looks like {name1: email1, name2: email2} and the other looks like {email1: name1, email2: name2}.

Hi @moriturus7

I also have a question regarding this practice problem. This is my first time posting so please let me know if I’m doing it wrong.

How come the code below does not work? I’ve ran the code and it just gives me me one letter as a key and one letter as the value. Is it because I cannot access the dictionary’s key and value by using using the index row[1] and row[0]?

for row in name_to_email:
email = row[1]
name = row[0]
email_to_name[email] = name

print(email_to_name)

Thank you for your help.

Hi, Anthony.

Yes, when you iterate through a dictionary you only go through its Keys
If you have a dictionary of the form

test_dict = {'max': '[email protected]', 'anthony': '[email protected]'}
for row in test_dict:
    print(row)

The resulting output will be in the form of
max
anthony

to get both key and value you need to use the items() method or access your first dictionary by key

for key, value in test_dict.items():
   dict_test[value] = key

OR

for key in test_dict:
   value = test_dict[key]
   dict_test[value] = key
2 Likes

Okay I see, thank you very much!