Lab 4 of 5
Β·
Semi-Guided
Filter Devices by Type Using List Comprehension
Objective: Use a list comprehension to extract only switch names from a mixed inventory.
Instructions
Create a file called lab4.py. Using the inventory below, use a list comprehension to build a list of switch names only. Then print the list and the total count.
inventory = [
{"name": "sw-core-01", "type": "switch"},
{"name": "rtr-edge-01", "type": "router"},
{"name": "sw-access-01","type": "switch"},
{"name": "fw-dmz-01", "type": "firewall"},
{"name": "sw-access-02","type": "switch"},
]
Your output must match exactly:
['sw-core-01', 'sw-access-01', 'sw-access-02']
Total switches: 3
Run it with python3 lab4.py. Paste your output below and submit.
π‘ Show Hint
List comprehension: switches = [d["name"] for d in inventory if d["type"] == "switch"]. Then print(switches) and print(f"Total switches: {len(switches)}")
Paste Your Output