Q. Nesting of dictionary allows you to store a dictionary inside another dictionary. Then why is following code raising error? What can you do to correct it?


d1 = {1 : 10, 2 : 20, 3: 30}

d2 = {1 : 40, 2 : 50, 3 : 60}

d3 = {1 : 70, 2 : 80, 3 : 90}

d4 = {d1 : "a", d2 : "b", d3 : "c"}


Answer =

Because here d1, d2 and d3 are as keys of dictionary d4, but we know that keys should be immutable data type. But here we provide dictionary as key which is mutable data type.

It can be corrected by replacing key and value of dictionary d4 like that:

d4 = {"a" : d1 , "b" : d2, "c" : d3}

2 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post