350-601 · Question #409
350-601 Question #409: Real Exam Question with Answer & Explanation
The correct answer is D: switch_id_list.update({"N9K-Spine-2": "1498323434"}). To add a new key-value pair to an existing Python dictionary named switch_id_list, the update() method is the correct approach for merging the new dictionary element.
Question
An engineer implements a Python script inside a Cisco Bash shell. The script contains this dictionary object: Which command must be used to add the 'NSK-Spine-2': `1498323434' element to the switch_id list dictionary?
Options
- Aswitch_id_list += {"N9K-Spine-2": "1498323434"}
- Bswitch_id_list = ["N9K-Spine-2": "1498323434"]
- Cswitch_id_list.append({"N9K-Spine-2": "1498323434"})
- Dswitch_id_list.update({"N9K-Spine-2": "1498323434"})
Explanation
To add a new key-value pair to an existing Python dictionary named switch_id_list, the update() method is the correct approach for merging the new dictionary element.
Common mistakes.
- A. The
+=operator is not used for merging dictionaries in this manner; it typically performs in-place addition for lists or concatenation for strings, and would result in a TypeError for dictionaries. - B. Assigning a new dictionary using
=would overwrite the existingswitch_id_listdictionary, not add an element to it, and the syntax["N9K-Spine-2": "1498323434"]is incorrect for creating a dictionary. - C. The
append()method is used to add an item to the end of a list, not to merge elements into a dictionary. Ifswitch_id_listwere a list,appendwould be applicable, but the question states it's a dictionary object.
Concept tested. Python dictionary manipulation (update method)
Reference. https://docs.python.org/3/library/stdtypes.html#dict.update
Topics
Community Discussion
No community discussion yet for this question.