Outputting Python Programs | Set 9 (dictionary)

dictionary = { ` GFG` : `python.engineering` ,

`google` : `google.com` ,

  `facebook` : ` facebook.com`

}

del dictionary [ `google` ]; 

for key, values ​​ in dictionary.items ():

print (key)

dictionary.clear (); 

for key, values ​​ in dictionary.items ():

print (key)

del dictionary; 

for key, values ​​ in dictionary.items ():

print (key)

a) and b and e
b) Runtime error
c) GFG
facebook
d) facebook
GFG

Resp. (a)
Output:

 facebook GFG 

Explanation: Claim: Del Dictionary; deletes the entire dictionary, so iterating over the remote dictionary results in a runtime error like this:

 Traceback (most recent call last): File "cbeac2f0e35485f19ae7c07f6b416e84.py", line 12, in for key, values ​​in dictionary.items (): NameError: name `dictionary` is not defined 

2) What is the output of the following program?

dictionary1 = { `Google` : 1 ,

` Facebook` : 2 ,

  `Microsoft` : 3

}

dictionary2 = { `GFG` : 1 ,

` Microsoft` : 2 ,

`Youtube` : 3

  }

dictionary1.update (dictionary2); 

for key, values ​​ in dictionary1.items ():

print (key, values)

a) compilation error
b) Runtime error
c) (“Google”, 1)
(“Facebook”, 2)
(& # 39 ; Youtube & # 39 ;, 3)
("Microsoft", 2)
("GFG", 1)
d) none of them

Resp. (c)
Explanation: dictionary1.update (dictionary2) is used to update dictionary1 entries with dictionary2 entries. If two dictionaries have the same keys, the value in the second dictionary is used. 

3) What is the output of the following program?

dictionary1 = { ` GFG` : 1 ,

` Google` : 2 ,

`GFG` : 3

}

print (dictionary1 [ `GFG` ]); 

a) Compilation error due to duplicate keys
b) Runtime error due to duplicate keys
c) 3
d) 1

Ans. (с)
Explanation: Here GFG — duplicate key. Duplicate keys are not allowed in Python. If the dictionary contains the same keys, then this key is assigned the most recently assigned value .

4) What is the output of the following program?

temp = dict ()

temp [ `key1` ] = { `key1` : 44 , `key2` : 566 }

temp [ `key2` ] = [ 1 , 2 , 3 , 4 ]

for (key, values) in temp.items ():

print (values, end = "")

a) compilation error
b) {& # 39; key1 & # 39 ;: 44, & # 39; key2 & # 39 ;: 566} [1, 2, 3, 4]
c) Runtime error
d) none of the above

Ans. (B)
Explanation: A dictionary can contain any value, such as an integer, string, list, or even another dictionary containing key-value pairs. 
Note: this code can only be executed in Python versions above 3

5) What is the output of the following program?

temp = { ` GFG` : 1 ,

`Facebook` : 2 ,

`Google` : 3

  }

for (key, values) in temp.items ():

print (key, values, end = " " )

a) Google 3 GFG 1 Facebook 2
b) Facebook 2 GFG 1 Google 3
c) Facebook 2 Google 3 GFG 1
d) any of the above

Ans. (D)
Explanation: The dictionaries are not ordered. Thus, any key-value pairs can be added anywhere in the dictionary. Any way out can come. 
Note: this code can only be executed in Python versions higher than 3.

This article is courtesy of Mayank Kumar . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting the article [email protected] … See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you`d like to share more information on the topic discussed above.