BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

Logic behind name_to_email[name] = email

Hi!

As the title says, im having a hard time trying to get the logic behind what the pc is doing here: name_to_email[name] = email. I get that its assigning the value “email” to the keys of the dictionary. But in nowhere were assigning the keys itself. So whats the logic behind this operation?

Screen Link:
https://app.dataquest.io/m/1011/python-fundamentals-practice-problems/7/converting-a-csv-to-a-dictionary

My Code:

name_to_email = {}
for row in rows:
    name = row[0]
    email = row[1]
    name_to_email[name] = email
    
william_email = name_to_email["William Chase"]
madison_email = name_to_email["Madison Wallace"]

What I expected to happen:

What actually happened:

Replace this line with the output/error

Python does that for you.

name_to_email[name] = email

The name_to_email[name] bit will create the key corresponding to name and then save the email as a value to that key.

1 Like

key is defined here:

name = row[0]