Skip to content
QuiznExam

List Comprehensions in Python

Test your knowledge of list comprehensions in Python, including syntax, expressions, conditions, nested comprehensions, filtering, transformations, and common use cases.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is a list comprehension in Python?

    • A. A concise way to create a list using an expression and an iterable
    • B. A method for sorting a list
    • C. A function that converts a list into a tuple
    • D. A special type of dictionary
    Show answer

    A. A concise way to create a list using an expression and an iterable

    List comprehensions provide a compact syntax for creating lists by applying an expression to items from an iterable.

  2. 2. Which syntax correctly represents a basic list comprehension?

    • A. [expression for item in iterable]
    • B. [for item in iterable expression]
    • C. [expression in item for iterable]
    • D. [item expression for in iterable]
    Show answer

    A. [expression for item in iterable]

    The basic list comprehension syntax places the expression first, followed by the for clause.

  3. 3. What is the output of the following code? [x * 2 for x in range(4)]

    • A. [0, 2, 4, 6]
    • B. [2, 4, 6, 8]
    • C. [0, 1, 2, 3]
    • D. [1, 2, 4, 8]
    Show answer

    A. [0, 2, 4, 6]

    range(4) produces 0, 1, 2, and 3, and each value is multiplied by 2.

  4. 4. Which keyword is used to iterate over items in a list comprehension?

    • A. for
    • B. loop
    • C. iterate
    • D. each
    Show answer

    A. for

    The for keyword introduces the iteration portion of a list comprehension.

  5. 5. How can a list comprehension filter items based on a condition?

    • A. By adding an if condition after the for clause
    • B. By adding an else clause before for
    • C. By using filterOnly before the expression
    • D. By placing the condition outside the brackets
    Show answer

    A. By adding an if condition after the for clause

    A filtering condition can be added after the for clause, such as [x for x in numbers if x > 0].

  6. 6. What is the output of the following code? [x for x in range(6) if x % 2 == 0]

    • A. [0, 2, 4]
    • B. [1, 3, 5]
    • C. [2, 4, 6]
    • D. [0, 1, 2, 3, 4, 5]
    Show answer

    A. [0, 2, 4]

    The condition keeps only values whose remainder when divided by 2 is zero.

  7. 7. Which list comprehension converts every string in names to uppercase?

    • A. [name.upper() for name in names]
    • B. [upper(name) in names]
    • C. [names.upper() for name]
    • D. [for name in names: name.upper()]
    Show answer

    A. [name.upper() for name in names]

    The expression name.upper() is applied to each item produced by iterating over names.

  8. 8. What does the following list comprehension produce? [x for x in range(5) if x > 2]

    • A. [3, 4]
    • B. [2, 3, 4]
    • C. [0, 1, 2]
    • D. [0, 1, 2, 3, 4]
    Show answer

    A. [3, 4]

    The values in range(5) are 0 through 4, and only 3 and 4 satisfy the condition x > 2.

  9. 9. Can a list comprehension contain more than one for clause?

    • A. Yes
    • B. No
    • C. Only when using tuples
    • D. Only when using dictionaries
    Show answer

    A. Yes

    List comprehensions can contain multiple for clauses, which can be useful for iterating over nested iterables.

  10. 10. What is the output of the following code? [(x, y) for x in [1, 2] for y in [3, 4]]

    • A. [(1, 3), (1, 4), (2, 3), (2, 4)]
    • B. [(1, 2), (3, 4)]
    • C. [1, 2, 3, 4]
    • D. [(1, 3), (2, 4)]
    Show answer

    A. [(1, 3), (1, 4), (2, 3), (2, 4)]

    The nested for clauses produce every combination of an x value with a y value.

  11. 11. Which expression uses a conditional expression inside a list comprehension?

    • A. ["even" if x % 2 == 0 else "odd" for x in numbers]
    • B. [for x in numbers if x % 2 == 0 else]
    • C. [if x % 2 == 0 for x in numbers else]
    • D. [x if for x in numbers else]
    Show answer

    A. ["even" if x % 2 == 0 else "odd" for x in numbers]

    A conditional expression can be placed in the expression portion of a list comprehension using the if-else syntax.

  12. 12. What type of object does a list comprehension return?

    • A. A list
    • B. A tuple
    • C. A set
    • D. A generator
    Show answer

    A. A list

    List comprehensions always create and return a list.

  13. 13. Which traditional loop is generally equivalent to this list comprehension? squares = [x * x for x in numbers]

    • A. A for loop that appends x * x to a list
    • B. A while loop that removes items
    • C. An if statement without a loop
    • D. A switch statement
    Show answer

    A. A for loop that appends x * x to a list

    The comprehension is a concise alternative to creating an empty list, looping through numbers, calculating x * x, and appending each result.

  14. 14. Which statement about list comprehensions is generally correct?

    • A. They can make simple list-building operations more concise
    • B. They can only process numeric lists
    • C. They always execute faster than every equivalent loop
    • D. They cannot contain conditions
    Show answer

    A. They can make simple list-building operations more concise

    List comprehensions provide concise and readable syntax for many list construction and filtering tasks, although a regular loop may be clearer for complex logic.

  15. 15. Which comprehension creates a list containing the lengths of the strings in words?

    • A. [len(word) for word in words]
    • B. [words.len() for word]
    • C. [length for words in len]
    • D. [for word in words: len]
    Show answer

    A. [len(word) for word in words]

    The len() function is applied to each word while iterating through the words iterable.