An automation engineer reviews a Python inventory script that loops over a list of hostnames, looks each one up in a dictionary of assigned roles, and prints a hostname only when the role equals 'spine'. For inventory {'leaf1': 'leaf', 'spine1': 'spine', 'spine2': 'spine'} and hosts ['leaf1', 'spine1', 'spine2'], which output does the script produce?
Select an answer to reveal the explanation.
Short Explanation
Your conditional acts like a sieve, letting only hostnames whose dictionary value is spine reach the print call. The loop walks the list top to bottom, so the survivors keep list order and leaf1 never shows up. That's the whole decision point, and it all hangs on the equality test.
Full Explanation
The output follows from how a for loop, a dictionary lookup and a conditional combine: the loop walks the host list in order, the dictionary returns the role stored for each hostname, and the equality test against 'spine' decides whether the print call runs. Only spine1 and spine2 pass that test, and because a plain loop never reorders its source, they emerge exactly as they were listed. Looping over every host without applying the role filter fails the requirement that only spine-role devices appear, so leaf1 would print even though its value is 'leaf'. Sorting the output alphabetically fails the loop-order fact, because Python does not sort dictionary keys or list items when you simply read them; ordering changes only when a script deliberately calls sorted(). Stopping after the first match fails the filtering concept, since a conditional guarding a print statement continues the loop rather than terminating it, and a break would be needed to halt early. Exam caveat: the script assumes every listed host exists as a key, because a missing key raises KeyError instead of printing anything. Practical check: run the loop in a REPL, collect matches in a list, then print the length to confirm both hits were recorded.