foo = {1:'1', 2:'2', 3:'3'} del foo[1] foo[1] = '10' del foo[2] print len(foo)
-
2
-
7
-
3
-
4
-
An exception is thrown
A
Correct answer
Explanation
The code starts with a dictionary of 3 key-value pairs. After 'del foo[1]', only keys 2 and 3 remain. Then 'foo[1] = '10'' adds a new key 1. After 'del foo[2]', only keys 1 and 3 remain. So len(foo) returns 2. The key takeaway is that deleting and re-adding the same key is allowed - the old key-value pair is gone, and adding it back counts as a new key.