nerdexam
Cisco

350-601 · Question #409

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…

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.

Automation

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?

Exhibit

350-601 question #409 exhibit

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"})

How the community answered

(23 responses)
  • B
    4% (1)
  • C
    4% (1)
  • D
    91% (21)

Why each option

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.

Aswitch_id_list += {"N9K-Spine-2": "1498323434"}

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.

Bswitch_id_list = ["N9K-Spine-2": "1498323434"]

Assigning a new dictionary using `=` would overwrite the existing `switch_id_list` dictionary, not add an element to it, and the syntax `["N9K-Spine-2": "1498323434"]` is incorrect for creating a dictionary.

Cswitch_id_list.append({"N9K-Spine-2": "1498323434"})

The `append()` method is used to add an item to the end of a list, not to merge elements into a dictionary. If `switch_id_list` were a list, `append` would be applicable, but the question states it's a dictionary object.

Dswitch_id_list.update({"N9K-Spine-2": "1498323434"})Correct

In Python, the `update()` method is used to add items from another dictionary (or an iterable of key-value pairs) to the current dictionary. It efficiently merges the new dictionary `{'N9K-Spine-2': '1498323434'}` into `switch_id_list`, adding the new key-value pair as required.

Concept tested: Python dictionary manipulation (update method)

Source: https://docs.python.org/3/library/stdtypes.html#dict.update

Topics

#Python#Dictionaries#Data Structures#Automation Scripting

Community Discussion

No community discussion yet for this question.

Full 350-601 Practice