Question #93275

Assume that d has been initialized as an empty dictionary:
d = {}
Which of the following generates an error?
d[12] = 5
d["1,2"] = 5
d[(1,2)] = 5
d[[1,2]] = 5

Expert's answer

d[12] = 5        - ok, result of the operation is d = {12: 5}

d["1,2"] = 5    - ok, result of the operation is d = {'1,2': 5}

                         total result is d = {12: 5, '1,2': 5}

d[(1,2)] = 5     - ok, result of the operation is d = {(1,2): 5}

                         total result is d = {12: 5, '1,2': 5, (1,2): 5}

d[[1,2]] = 5     - error, list cannot be in the index of dictionary

 

Answer

 

d[[1,2]] = 5 generates an error


LATEST TUTORIALS
APPROVED BY CLIENTS