While developing a Python 3 monitoring tool for Linux servers, you store disk-usage percentages in a dictionary named usage where each key is the mount point and each value is an integer (0-100). You must create a new dictionary named critical that includes only the mount points whose usage exceeds 80 percent, assigning each of those keys the string 'CRITICAL'. Which single line of code accomplishes this task by using a dictionary comprehension?
critical = {mp for mp, pct in usage.items() if pct > 80: 'CRITICAL'}
critical = {mp:'CRITICAL' for mp, pct in usage.items() if pct > 80}
critical = [mp:'CRITICAL' for mp, pct in usage.items() if pct > 80]
critical = dict(mp='CRITICAL' for mp, pct in usage.items() if pct > 80)
Dictionary comprehensions are written with curly braces and use the syntax {key_expression: value_expression for key, value in iterable if condition}. The statement critical = {mp:'CRITICAL' for mp, pct in usage.items() if pct > 80} correctly satisfies this syntax: it iterates through usage.items(), filters for items whose percentage is above 80, and builds a new dictionary with the qualifying mount point as the key and the literal string 'CRITICAL' as the value.
An answer choice that uses square brackets ([]) is incorrect because it would attempt to build a list, and the key:value syntax is invalid for a list comprehension. An answer that misuses the dict() constructor with an invalid generator expression would result in a syntax error. Lastly, placing the colon for the key-value pair after the for and if clauses is also invalid Python syntax.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What is a dictionary comprehension in Python?
Open an interactive chat with Bash
What does the `usage.items()` method do in Python?
Open an interactive chat with Bash
Why are curly braces `{}` required for dictionary comprehensions?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access