Sharpen your backend Python skills with these essential, production-proven one-liners for safe, efficient, and clean code practices.
Which one-liner allows you to retrieve a value from a dictionary safely, returning a default if the key does not exist?
Explanation: Using my_dict.get('key', 'default') safely accesses dictionary values and returns a default if the key is missing. my_dict['key'] throws a KeyError if the key is absent. my_dict.pop('key') removes the item and fails if not present. my_dict.find('key') is not a valid method for dictionaries.
What is the most concise way to filter even numbers from a list in Python using a one-liner?
Explanation: [x for x in numbers if x % 2 == 0] is the most readable and concise one-liner to filter even numbers. filter(lambda x: x % 2, numbers) filters out even numbers incorrectly (keeps odds). map returns a list of booleans, not filtered values. [x % 2 == 0 for x in numbers] creates a list of True/False, not actual numbers.
Which one-liner assigns a default value to a variable only if it is currently None?
Explanation: value = value or 'default' assigns 'default' if value is None or any falsy value. The ternary version works but is less concise. The if statement is not a true one-liner. value = value and 'default' would return 'default' only if value is truthy, which is often incorrect for this use.
How can you reverse a string in Python in a single line?
Explanation: original[::-1] quickly reverses a string using slicing. original.reverse() and ''.reverse(original) are not valid string methods. reverse(original) does not exist in standard Python.
Which Python one-liner can flatten a 2D list of lists into a single list?
Explanation: [item for sublist in lists for item in sublist] flattens 2D lists via nested comprehension. lists.flatten() and flatten(lists) are not standard methods. sum(lists) tries to sum lists, raising an error.
Sharpen your backend Python skills with these essential, production-proven one-liners for safe, efficient, and clean code practices.
This quiz contains 5 questions. Below is a complete reference of all questions, answer choices, and correct answers. You can use this section to review after taking the interactive quiz above.
Which one-liner allows you to retrieve a value from a dictionary safely, returning a default if the key does not exist?
Correct answer: value = my_dict.get('key', 'default')
Explanation: Using my_dict.get('key', 'default') safely accesses dictionary values and returns a default if the key is missing. my_dict['key'] throws a KeyError if the key is absent. my_dict.pop('key') removes the item and fails if not present. my_dict.find('key') is not a valid method for dictionaries.
What is the most concise way to filter even numbers from a list in Python using a one-liner?
Correct answer: [x for x in numbers if x % 2 == 0]
Explanation: [x for x in numbers if x % 2 == 0] is the most readable and concise one-liner to filter even numbers. filter(lambda x: x % 2, numbers) filters out even numbers incorrectly (keeps odds). map returns a list of booleans, not filtered values. [x % 2 == 0 for x in numbers] creates a list of True/False, not actual numbers.
Which one-liner assigns a default value to a variable only if it is currently None?
Correct answer: value = value or 'default'
Explanation: value = value or 'default' assigns 'default' if value is None or any falsy value. The ternary version works but is less concise. The if statement is not a true one-liner. value = value and 'default' would return 'default' only if value is truthy, which is often incorrect for this use.
How can you reverse a string in Python in a single line?
Correct answer: reversed_string = original[::-1]
Explanation: original[::-1] quickly reverses a string using slicing. original.reverse() and ''.reverse(original) are not valid string methods. reverse(original) does not exist in standard Python.
Which Python one-liner can flatten a 2D list of lists into a single list?
Correct answer: [item for sublist in lists for item in sublist]
Explanation: [item for sublist in lists for item in sublist] flattens 2D lists via nested comprehension. lists.flatten() and flatten(lists) are not standard methods. sum(lists) tries to sum lists, raising an error.