Problem 6 : Using the Same Key Multiple Times

Screen Link:
https://app.dataquest.io/m/1011/python-fundamentals-practice-problems/6/using-the-same-key-multiple-times

My Code:
keys = [1, 2, 1, 3, 1, 4]
values = [1, 2, 3, 4, 5, 6]
d = {}
for i in range(len(keys)):
d[keys[i]] = values[i]

Replace this line with your code

What I expected to happen:

What actually happened:
Answer = 5

Replace this line with the output/error

Hi DQ Team,

I am very new to programming and DQ and doing the problem sets to understand better. For problem 6, is there anyone can explain the solution in detail? I am still not clear with the answer given.

Thank you for your time.
JJ

I want you to break your code down line-by-line first.

For each line, write down what you think is happening in that code line. Think about what that line of code does and write that down.

This is a very important step. Because it’s very easy for someone more experienced than you to just provide you the answer/explanation here, but that doesn’t necessarily ensure long-term learning that benefits you.

So,

  • Read the code, line-by-line
  • Write down what each line of code does
  • When you get stuck and can’t explain to yourself what something in the code does, use basic print statements to test out how the variables in your code are being affected by that code line
  • Still can’t figure things out, ask about that code line specifically.

For this particular problem, you would require an understanding of -

  • Lists
    • including how to access elements in a list using indexing
  • Dictionaries
    • including how to add/update the keys or values in a dictionary
  • For loops
  • The functions len() and range()

If you have any confusion about any of the above topics/concepts, then I first recommend that you make sure you understand those basics and tackle the problem again.

But start with explaining what you understand of the code so far.

3 Likes

Hello,

I am a newbie and came across this very same problem and issue with comprehending the solution. To me, I thought d[1] solution would be “2:2”. How is the solution “5”?

Thanks in advance.

Did you try to follow my suggestion in my comment above yours?

Yes, of course I did. Just FYI, I make sure I try everything I can possible, within my limited knowledge, before I post a question to the community. I understand how learning works, but sometimes getting the answer to how a solution was reached can really help the learning process.

Take a small example from the code -

keys   = [1, 2, 1]
values = [1, 2, 3]
d = {}
for i in range(len(keys)):
    d[keys[i]] = values[i]

Now, looking at the for loop, how does d get updated for each i?

When, i = 0, then keys[0] = 1 and values[0] = 1. So, d[1] = 1.

So, after the first iteration, the dictionary is -

d = {1:1}

For the second iteration, there is not much difference -

When, i = 1, then keys[1] = 2 and values[1] = 2. So, d[2] = 2.

So, after the second iteration, the dictionary is -

d = {1:1, 2:2}

For the third iteration,

When, i = 2, then keys[2] = 1 and values[2] = 3. So, d[1] = 3.

This is where things change. The key for d above is 1. But that key already exists for d. So, we are essentially changing the value corresponding to that key in d.

So, after the third iteration, the dictionary is -

d = {1:3, 2:2}

Now, go through the full code again with the following lists -

keys   = [1, 2, 1, 3, 1, 4]
values = [1, 2, 3, 4, 5, 6]

What happens when i = 4?

5 Likes

Thank you so much for going through the process in a very thorough manner @the_doctor !! This is very helpful and clear. I forgot that each key has to have a unique value, so whenever the same key with a new value is run, the key gets updated with the new value. It makes sense now.

To answer your question, when i = 4, the d dictionary should be {1:5, 2:2, 3:4} and furthermore, when i = 5, the d dictionary should be {1:5, 2:2, 3:4, 4:6}.

Thank you for taking time to write all that out :pray: