1

Here is a basic question for you awesome folks out there. I'm a fairly new person to coding and when I saw this code, I just couldn't figure it out. Here's the question: Why is profile[key] = value in that specific loop? It seems like this code is making the dictionary key into a value which doesn't make sense in my head, any explanation would be great! The code:

def build_profile(first, last, **user_info):
   """Build a dictionary containing everything we know about a user"""

    profile = {}
    profile["first_name"] = first
    profile["last_name"] = last

    for key, value in user_info.items():
        profile[key] = value  # Why is this converting the key of the dictionary into a value?
    return profile

user_profile = build_profile("albert", "einstein",
                             location="princeton",
                             field="physics")

print(user_profile)

P.S. This is on page 153 of "Python Crash Course"β€”It gives an explanation but I just don't understand it, sorry.

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
User31899
  • 13
  • 4

2 Answers2

0

You are misunderstanding what profile[key] = value does. Dictionaries are composed of key, value pairs.

# when you do this:
for key, value in user_info.items(): #should be user_info.iteritems() because dict.items() is deprecated.
    profile[key] = value

# you are basically copying the user_info dict, which can be done much easier this way:
profile = user_info.copy()

So profile[key] = value means, in English, that you are creating a key in the dictionary profile and assigning it to a value. You can access a value stored in a dictionary with dictionary[key].

sam-pyt
  • 1,027
  • 15
  • 26
  • Ohh, wow, you just made that code so much cleaner, thank you! I guess I'm seeing the "profile[key]" as a variable thus, profile[key] is the 'box' in which the 'value' is being placed in. But it seems like you're saying the "profile[key] is not holding the variable "value" but is simply part of the dictionary syntax, is that correct? – User31899 Nov 26 '17 at 04:06
0

It's not converting anything, I think you may be confused a little bit about what a dictionary is.

Specifically, a dictionary is a collection of key and value pairs.

i.e. if it was a list, it would look like this:

[("first_name", "albert"),
 ("last_name", "einstein"),
 ("location", "princeton"),
 ("field", "physics")]

What is happening inside the loop is (in pseudocode):

foreach function_argument # (e.g. location="princeton")
    get the parameter name # (e.g. "location")
    get the argument value # (e.g. "princeton")

    create a new key-value pair in the profile: # (profile[key] = value)
        the key = the parameter name
        the value = the argument value

You may find understanding the difference between a parameter and an argument helpful.

Vikram Saran
  • 1,143
  • 8
  • 17
  • Interesting, so **kwargs are actually structured as a dictionary_type? I thought they were formatted as a tuple_type, or perhaps I'm thinking of *args? – User31899 Nov 26 '17 at 04:01