Python List Comprehension Quiz

Last Updated :
Discuss
Comments

Question 1

In list comprehension, what does the expression represent?

  • A

    The iterable being traversed.

  • B

    The condition for filtering elements.

  • C

    The value to be included in the new list.

  • D

    The index of the current element.

Question 2

Can list comprehension be used with other data types besides lists?

  • A

    No, it can only be used with lists.

  • B

    Yes, it can be used with any iterable.

  • C

    Yes, but only with strings.

  • D

    No, it is limited to numerical data types.

Question 3

Explain the concept of filtering in list comprehension.

  • A

    It involves transforming elements based on a specified condition.

  • B

    It refers to the process of excluding elements based on a condition.

  • C

    It is a technique for optimizing list comprehension.

  • D

    It is not applicable to list comprehension.

Question 4

What will the following list comprehension output?

Python
squares = [x**2 for x in range(5)]
print(squares)
  • A

    [0, 1, 4, 9, 16]

  • B

    [1, 4, 9, 16, 25]

  • C

    [0, 2, 4, 6, 8]

  • D

    [1, 3, 5, 7, 9]

Question 5

What is the output of the following list comprehension?

Python
vowels = ['a', 'e', 'i', 'o', 'u']
upper_vowels = [vowel.upper() for vowel in vowels]
print(upper_vowels)
  • A

    ['a', 'e', 'i', 'o', 'u']

  • B

    ['A', 'E', 'I', 'O', 'U']

  • C

    ['a', 'E', 'i', 'O', 'u']

  • D

    ['A', 'e', 'I', 'o', 'U']

Question 6

When using list comprehension, what happens if the iterable is empty?

  • A

    It raises a ValueError.

  • B

    It creates an empty list.

  • C

    It raises a SyntaxError.

  • D

    It results in an infinite loop.

Question 7

What is the purpose of the following list comprehension?

Python
numbers = [1, -2, 3, -4, 5]
squared_positives = [x**2 if x > 0 else 0 for x in numbers]
print(squared_positives)
  • A

    Squares all numbers and preserves negative results.

  • B

    Squares positive numbers and replaces non-positive values with 0.

  • C

    Squares only negative numbers.

  • D

    Keeps only numbers greater than 3.

Question 8

Does list comprehension automatically remove duplicate values? Consider the following code.

Python
values = [2, 2, 3, 3, 3]
result = [x for x in values if x > 2]
print(result)
  • A

    [2, 2, 3, 3, 3]

  • B

    [3]

  • C

    [3, 3, 3]

  • D

    [2, 3]

Question 9

How can list comprehension be used to create a list of tuples?

  • A

    By converting the entire resulting list into a tuple using tuple().

  • B

    By wrapping each output element in a list.

  • C

    List comprehension cannot create tuple elements.

  • D

    By returning a tuple expression, such as (x, y), for each iteration.

Question 10

What is the significance of the order of for clauses in nested list comprehensions?

  • A

    The order has no effect on the result.

  • B

    It determines the order in which elements and combinations are generated.

  • C

    It is not possible to have multiple for clauses in list comprehension.

  • D

    It only affects the readability of the code.

There are 25 questions to complete.

Take a part in the ongoing discussion