Automation and Artificial Intelligence

A script copies a nested device list, then modifies the copy. What does it print? devices = [["R1"], ["R2"]] selected = devices[:] selected[0].append("up") selected.append(["R3"]) print(len(devices), devices[0])

Show the answer

Correct answerA. 2 ['R1', 'up']

AWhy this is right: The original outer list keeps two elements, and its shared first inner list now contains 'R1' and 'up'.

BWhy not this: The outer append affects only selected, while the nested append affects the shared first inner list.

CWhy not this: The nested lists remain shared, so appending 'up' is visible through devices.

DWhy not this: The outer lists are separate, so adding a device to selected does not increase the length of devices.

The slice creates a shallow copy: the outer lists are separate, but their nested lists are shared. Appending 'up' changes the shared nested list; appending ['R3'] changes only selected. A deep copy would also separate the nested lists.