List comprehensions in Python provide a concise, readable, and highly optimized syntax for creating new lists based on existing iterables. By combining loops and conditional filters into a single line enclosed within square brackets, they often replace traditional for loops and map/filter functions. Understanding how to construct, read, and nest list comprehensions is vital for writing Pythonic, high-performance code. These practice MCQs test your ability to evaluate syntax, handle conditions, and analyze code outputs effectively.
Python List Comprehension MCQs
1 min read
Correct Answer: a) List comprehensions use square brackets while generator expressions use parentheses
Explanation:
List comprehensions are enclosed in square brackets [] and evaluate eagerly, whereas generator expressions use parentheses () and evaluate lazily.
Correct Answer: a) [ord(c) for c in 'abc']
Explanation:
The ord() function returns the integer ordinal of a character, whereas chr() converts an integer to its character equivalent.
Correct Answer: a) [3, 4]
Explanation:
range(5) yields 0, 1, 2, 3, 4. The filter 'if x > 2' keeps only numbers strictly greater than 2, resulting in [3, 4].
Correct Answer: a) By chaining multiple 'if' keywords consecutively
Explanation:
Chained 'if' clauses act with an implicit logical AND condition.
Correct Answer: a) [2, 1, 0, 1, 2]
Explanation:
This is a ternary conditional expression mapping that computes the absolute value for each element.
Correct Answer: a) Loop variables are local to the list comprehension and do not leak into the surrounding scope
Explanation:
Python 3 isolates list comprehension scopes, preventing loop variables from leaking into the parent block.
Correct Answer: a) [0, 1, 2, 0, 1, 2]
Explanation:
Multiplying a list by 2 performs list concatenation, repeating its elements twice.
Correct Answer: a) ['a', 'b', 'c']
Explanation:
The split() method splits the string by whitespace into a list of words, which are then iterated over.
Correct Answer: a) No, 'break' and 'continue' statements are syntax errors inside list comprehensions
Explanation:
Control flow statements like break and continue cannot be used directly inside list comprehension syntax.
Correct Answer: a) [0, 2, 4]
Explanation:
First, the list comprehension generates even numbers up to 9: [0, 2, 4, 6, 8]. Then slicing [:3] takes the first three elements.
Correct Answer: a) [False, True]
Explanation:
0 evaluates to False and 1 evaluates to True in boolean contexts.
Correct Answer: a) List comprehensions are generally faster because the loop is executed in C bytecode
Explanation:
Python's interpreter executes list comprehensions with optimized C-level iteration constructs.
Correct Answer: a) ['apple', 'banana']
Explanation:
The strip() method removes leading and trailing whitespaces from each string.
Correct Answer: a) [0, 1, 2]
Explanation:
The inner loop runs once per outer iteration, yielding each item of range(3).
Correct Answer: a) {key: value for item in iterable}
Explanation:
Dictionary comprehensions use curly braces {} with key-value pairs separated by a colon.
Correct Answer: a) [1, 2, 3]
Explanation:
The int() function converts each character string into an integer object.
Correct Answer: a) [1, 9]
Explanation:
Odd numbers in range(4) are 1 and 3. Squaring them yields 1**2 = 1 and 3**2 = 9.
Correct Answer: a) []
Explanation:
Iterating over an empty iterable produces an empty list.
Correct Answer: a) {x % 3 for x in range(10)}
Explanation:
Curly braces with an expression and iteration clause construct a set comprehension, which automatically removes duplicates.
Correct Answer: a) [6, 4]
Explanation:
Length of 'python' is 6 and length of 'code' is 4.
Correct Answer: a) ['a', 'b', 'c']
Explanation:
Iterating over a string yields its individual characters as single-character strings.
Correct Answer: a) Yes, arbitrarily nested comprehensions are syntactically valid
Explanation:
List comprehensions can be nested as deeply as needed, though deep nesting harms code readability.
Correct Answer: a) [4, 6]
Explanation:
zip() pairs elements in parallel: 1+3=4 and 2+4=6.
Correct Answer: a) ['HI', 'THERE']
Explanation:
The upper() method converts string characters to uppercase.
Correct Answer: a) A SyntaxError is raised
Explanation:
Python syntax mandates that an expression must precede the 'for' keyword.
Correct Answer: a) [0]
Explanation:
not x is True only when x is 0 (falsy value).
Correct Answer: a) enumerate()
Explanation:
enumerate() yields (index, element) tuples which can be unpacked in the loop header.
Correct Answer: a) [3.14, 2.72]
Explanation:
The round() function rounds floats to the specified number of decimal places.
Correct Answer: a) [0, 2]
Explanation:
Filters even numbers from range(4), resulting in 0 and 2.
Correct Answer: a) Yes, because expressions and function calls can execute side effects
Explanation:
While syntactically possible, relying on side effects in comprehensions is considered non-idiomatic.
Correct Answer: a) ['aaa', 'bbb']
Explanation:
Multiplying a string by 3 repeats its characters 3 times.
Correct Answer: a) [, , ]
Explanation:
The type() function returns the class type object for each element.
Correct Answer: a) [2, 3]
Explanation:
Numbers strictly between 1 and 4 in range(5) are 2 and 3.
Correct Answer: a) [item for row in matrix for item in row]
Explanation:
The nested loop order matches standard nested for loops: outer loop first, inner loop second.
Correct Answer: a) [1, 2]
Explanation:
ord('A') is 65 (65-64=1) and ord('B') is 66 (66-64=2).
Correct Answer: a) ['a', 'b']
Explanation:
Filters out the hyphen character '-'.
Correct Answer: a) [1.0, 2.0]
Explanation:
The float() function converts integers to floating-point numbers.
Correct Answer: a) [0, 2]
Explanation:
Excludes 1 from range(3).
Correct Answer: a) Yes, because file objects are iterables yielding lines sequentially
Explanation:
Iterating over an open file object yields each line, making it compatible with list comprehensions.
Correct Answer: a) [3, 1, 2]
Explanation:
The abs() function converts negative numbers to positive.
Correct Answer: a) [0, 1, 2, 3, 4]
Explanation:
The '+' operator concatenates the two lists.
Correct Answer: a) [0, 0, 0, 1]
Explanation:
Cartesian products: 0*0=0, 0*1=0, 1*0=0, 1*1=1.
Correct Answer: a) ['0', '1', '2']
Explanation:
Converts integers to string representations.
Correct Answer: a) ['e', 'o']
Explanation:
Filters characters in 'hello' that are present in the vowel string 'aeiou'.
Correct Answer: a) Yes, e.g., [a + b for a, b in [(1, 2), (3, 4)]]
Explanation:
Loop headers fully support sequence and tuple unpacking.
Correct Answer: a) ['cba', 'fed']
Explanation:
Slice [::-1] reverses each string.
Correct Answer: a) [1, 2]
Explanation:
0 evaluates to False and is filtered out, leaving [1, 2].
Correct Answer: a) [3, 7]
Explanation:
Applies sum() to each sublist: sum([1,2])=3 and sum([3,4])=7.
Correct Answer: a) [5, 6, 7]
Explanation:
Adds 5 to each element in range(3) (0, 1, 2).
Correct Answer: a) [0, 3, 6, 9]
Explanation:
Numbers from 0 to 9 divisible by 3 are 0, 3, 6, 9.
Correct Answer: a) ['Python', 'Java']
Explanation:
Capitalizes the first letter of each string.
Correct Answer: a) [True, False]
Explanation:
Preserves the boolean values.
Correct Answer: a) []
Explanation:
No numbers in range(3) satisfy x > 5.
Correct Answer: a) ['a', 'b']
Explanation:
Converts string characters to lowercase.
Correct Answer: a) [1, 3]
Explanation:
Selects odd numbers from range(4).
Correct Answer: a) [0, 1]
Explanation:
not True is False (0); not False is True (1).
Correct Answer: a) [1, 2, 3]
Explanation:
Computes the length of each sublist.
Correct Answer: a) [0, 1, 8]
Explanation:
Cubes of 0, 1, 2 are 0, 1, 8.
Correct Answer: a) ['1', '2']
Explanation:
The isdigit() method filters out non-digit characters.
Correct Answer: a) [2, 4]
Explanation:
Filtered elements > 0 are 1 and 2. Multiplying by 2 gives 2 and 4.
Correct Answer: a) [True, True]
Explanation:
An object's identity compared to itself is always True.
Correct Answer: a) [0, 0, 1, 1, 2, 2]
Explanation:
Each element of range(3) is repeated twice due to the inner loop over range(2).
Correct Answer: a) [0, 1, 0, 1]
Explanation:
Modulo 2 results for 0, 1, 2, 3 are 0, 1, 0, 1.
Correct Answer: a) ['p', 'y', 't', 'h', 'n']
Explanation:
Wait, 'python' vowels are 'o'. Wait! In 'python', vowels are 'o'. So 'not in aeoiu' excludes 'o', leaving 'p', 'y', 't', 'h', 'n'. Wait, let's check: 'p', 'y', 't', 'h', 'n'. Yes.
Correct Answer: a) [3, 4]
Explanation:
Filters elements > 1 (2 and 3), then adds 1 to each, yielding 3 and 4.
Correct Answer: a) [2.0, 3.0, 4.0]
Explanation:
Square roots of 4, 9, 16 as floats.
Correct Answer: a) []
Explanation:
None is falsy, so the filter condition is never met.
Correct Answer: a) [0, 2, 4]
Explanation:
Even numbers in range(5) are 0, 2, 4.
Correct Answer: a) [['hello', 'world'], ['python', 'code']]
Explanation:
Splits each string into a list of words, creating a nested list structure.
Correct Answer: a) [False, True, False]
Explanation:
Inverts each boolean value using the 'not' operator.
Correct Answer: a) [[], [0], [0, 1]]
Explanation:
For x=0: []; x=1: [0]; x=2: [0, 1].
Correct Answer: a) []
Explanation:
Multiplying a list by 0 yields an empty list.
Correct Answer: a) [1, 2, 3]
Explanation:
String lengths of integers converted to strings.
Correct Answer: a) ['a1', 'b1']
Explanation:
Combines characters from 'ab' with '1'.
Correct Answer: a) [True, False, True]
Explanation:
1 and -1 are truthy, 0 is falsy.
Correct Answer: a) [0, 3]
Explanation:
Numbers in range(5) divisible by 3 are 0 and 3.
Correct Answer: a) [1, 2]
Explanation:
Excludes 0 from range(3).
Correct Answer: a) [2, 3, 4]
Explanation:
Adds 2 to each element in range(3).
Correct Answer: a) [2]
Explanation:
Only 2 in range(3) is strictly greater than 1.
Correct Answer: a) [2, 4, 6]
Explanation:
List comprehensions always return a list object, multiplying each tuple element by 2.
Correct Answer: a) ['x', 'y', 'z']
Explanation:
Iterates over string characters into a list.
Correct Answer: a) [1, 3]
Explanation:
Selects odd numbers from range(4).
Correct Answer: a) [0, 0, 1, 1, 2, 2]
Explanation:
For each outer element (0, 1, 2), the inner loop runs twice, yielding duplicates of each outer element.
Correct Answer: a) [0, 1, 4]
Explanation:
Squares of 0, 1, 2 are 0, 1, 4.
Correct Answer: a) [65, 66]
Explanation:
Unicode integer values for 'A' and 'B'.
Correct Answer: a) [1]
Explanation:
Filters for elements equal to 1.
Correct Answer: a) [10, 11]
Explanation:
Adds 10 to 0 and 1.
Correct Answer: a) [0, 1, 2]
Explanation:
Lengths of empty string, 'a', and 'ab'.
Correct Answer: a) [0, 1]
Explanation:
Numbers in range(3) strictly less than 2 are 0 and 1.
Correct Answer: a) [0, 10, 20]
Explanation:
Multiplies 0, 1, 2 by 10.
Correct Answer: a) ['int', 'str', 'float']
Explanation:
Retrieves the string name of the class type for each object.
Correct Answer: a) [3]
Explanation:
Only 3 in range(4) is strictly greater than 2.
Correct Answer: a) [0, 1]
Explanation:
Pairs where x equals y: (0,0) yielding 0, and (1,1) yielding 1.
Related Posts
New
New
New

Python Strings MCQs
In Python, a string (str) is an immutable sequence of Unicode characters used to represent text data. Defined using single,…
August 27, 2026By MCQs Generator

JavaScript This Keyword & Scope MCQs
Variable scoping and declaration keywords (var, let, and const) determine where identifiers are visible and accessible within a JavaScript execution…
August 29, 2026By MCQs Generator

JavaScript Hoisting MCQs
Hoisting is a fundamental mechanism in JavaScript where variable and function declarations are notionally moved to the top of their…
August 29, 2026By MCQs Generator
Related Categories
New












AI & Data Science MCQ
5 topics
By MCQs Generator
New
Arts & Humanities MCQ
4 topics
By MCQs Generator
New
Civil Engineering MCQ
4 topics
By MCQs Generator
New
Commerce & Business MCQ
4 topics
By MCQs Generator
New
Competitive Exams MCQ
5 topics
By MCQs Generator
New
Electrical & Electronics Engineering MCQ
3 topics
By MCQs Generator
New
General Knowledge MCQ
2 topics
By MCQs Generator
New
General Science MCQ
4 topics
By MCQs Generator
New
Law & Judiciary MCQ
3 topics
By MCQs Generator
New
Mechanical Engineering MCQ
4 topics
By MCQs Generator
New
Medical & Health Sciences MCQ
4 topics
By MCQs Generator
New
Modern Tech Fields MCQ
3 topics
By MCQs Generator