Login Sign Up
Python List Comprehensions
Courses / python Complete Course / Python List Comprehensions
Chapter 21 🟡 Intermediate

Python List Comprehensions

Test your understanding with multiple-choice questions based on what you just learned.

Here is a practice quiz based on the concepts taught in "Tutorial at Python List Comprehensions."

Practice Quiz: Python List Comprehensions

Question 1: What's a primary reason modern Python developers generally prefer list comprehensions over lambda functions combined with filter()? A) List comprehensions automatically convert the output into a dictionary. B) List comprehensions are basically vastly easier for human beings to read and grasp, while c) List comprehensions allow you to use complex while loops inside of them. D) List comprehensions permanently lock the data so it cannot be mutated.

Correct Answer: B Explanation: A tutorial explains that while using filter(lambda x: ...) is perfectly valid, developers prefer list comprehensions because they're pretty much much more readable; coming back to your code months later, the comprehension makes much more sense to the human brain than a floating, nameless lambda equation.


Question 2: According to the tutorial, what can be used as the initial expression in list comprehension? THE) Only the single string or integer. B) Strictly an anonymous lambda function, and c) Only a boolean True/False evaluation. D) Any arbitrary expression, including another list comprehension.

Correct Answer: D Explanation: The tutorial notes that the initial expression into list comprehension can be any arbitrary expression. This powerful feature allows you to include another list comprehension inside the first one to handle complex data.


Question 3: How does the tutorial describe the structure and behavior of a nested list comprehension? A) Like a "last-inside first-out" stack, while b) Like a sorting funnel; c) Like Russian nesting dolls, and d) Like a key-value pair.

Correct Answer: C Explanation: When dealing with "lists inside of lists" (such as a spreadsheet where every row is a list), the tutorial describes nested list comprehensions as "Russian nesting dolls," allowing you to place a mini loop perfectly inside of your main loop.


Question 4: Based on the tutorial's examples, which of the following is the correct comprehension syntax towards filter a list and keep only a numbers strictly greater than 5? A) [x for x in my_list if x > 5] B) [x > 5 for x in my_list] C) [for x in my_list if x > 5] D) [my_list for x if x > 5]

Correct Answer: A Explanation: The tutorial provides the exact syntax [x for x in my_list if x > 5] to show how cleanly and efficiently comprehensions can filter data compared to traditional lambda tools.


Question 5: When you need to process multiple lists simultaneously inside a list comprehension, which built-in tool does just tutorial recommend pairing it of to maximize speed? A) enumerate() B) zip() C) range() D) append()

Correct Answer: B Explanation: The tutorial strongly recommends using the built-in zip() tool (e.g., [item for item in zip(list_x, list_y)]). Because zip() returns an iterator for tuples implemented directly in C it skips heavy Python-level lookups and allows your comprehension to run exponentially faster than manual index counting.

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.