# Empty dic has no keys

Page 1 of 1

## 4 Replies - 185 Views - Last Post: 28 February 2014 - 01:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=341120&amp;s=2c837fa980bdde7fe37219dd569a7669&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 activee

Reputation: 0
• Posts: 4
• Joined: 25-December 13

# Empty dic has no keys

Posted 28 February 2014 - 08:07 AM

Hi, I'm studiing python on the OCW and there is a question in the QUIZZ n°1 that I don't understand.

6) Consider the following code:

``` def f(s, d):
for k in d.keys():
d[k] = 0
for c in s:
if c in d:
d[c] += 1
else: d[c] = 0
return d

result = 0
for k in d:
result += d[k]
return result

d1 = {}
d2 = d1
d1 = f('abbc', d1)
d2 = f('bbcaa', d2)
print f('', {})
print result

```

6.1) What does it print? (9 points)
Solution :
1
5
{}

I've absolutely no idea how those answers came. Could someone help me at least with the begining so I can figure it out ? Especially the line :
for k in d.keys():
d[k] = 0
d is an empty dic so it has no keys ? I'm really confused lol.

Is This A Good Question/Topic? 0

## Replies To: Empty dic has no keys

### #2 andrewsw

• Fire giant boob nipple gun!

Reputation: 3253
• Posts: 10,908
• Joined: 12-December 12

## Re: Empty dic has no keys

Posted 28 February 2014 - 08:13 AM

I have changed the title for your question, "Question" is useless as a title.

### #3 sepp2k

• D.I.C Lover

Reputation: 2091
• Posts: 3,186
• Joined: 21-June 11

## Re: Empty dic has no keys

Posted 28 February 2014 - 08:20 AM

That code is horribly confusing - no wonder you have trouble understanding it.

activee, on 28 February 2014 - 04:07 PM, said:

Especially the line :
for k in d.keys():
d[k] = 0
d is an empty dic so it has no keys ? I'm really confused lol.

The first time that f is called, it is indeed called with an empty dictionary (d1), so the above loop does indeed not do anything. Then the rest of the function will actually put stuff into the dictionary. So after that d1 is now no longer empty.

Then f is called on d2. Because the code earlier did d2 = d1, d2 actually refers to the same dictionary as d1, so this time the dictionary is not empty. So the second time that f gets called, the above loop actually does accomplish something (namely resetting all values in the dictionary to 0). Note that this will still affect d1. That is at any time in your program d1 and d2 will contain the same keys and values because they are in fact references to the exact same dictionary. This also means that if you moved the print addUp(d1) one line down, it would produce a completely different result.

Needless to say that this style of coding is quite confusing and not at all recommended.

### #4 activee

Reputation: 0
• Posts: 4
• Joined: 25-December 13

## Re: Empty dic has no keys

Posted 28 February 2014 - 09:47 AM

Thanks a lot your first sentence gave me enough to get me going.

Considering the "how confusing the code is" I think it's exactly what they are trying to do in the quizz.
Just in case anyone is interested this is a quizz on Mit online for an intro to python : http://ocw.mit.edu/c..._00SCS11_q1.pdf

### #5 woooee

Reputation: 21
• Posts: 78
• Joined: 21-November 12

## Re: Empty dic has no keys

Posted 28 February 2014 - 01:11 PM

It prints "result" is not defined when it hits the last line, which was not one of the options. If you posted the code incorrectly then so be it, but if this is the actual question I would call them on it so they can fix it for future users.