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

In what scenarios might it be better to use traditional loops instead of list comprehension?

  • A

    When the code needs to be more concise.

  • B

    When the logic involves complex conditions.

  • C

    In situations where performance is critical.

  • D

    List comprehension is always preferable.

Question 3

What will the following list comprehension output?

Python
words = ['apple', 'banana', 'cherry']
word_lengths = {word: len(word) for word in words if len(word) % 2 == 0}


  • A

    {'apple': 5, 'banana': 6, 'cherry': 6}

  • B

    {'apple': 5, 'banana': 6}

  • C

    {'apple': 5, 'cherry': 6}

  • D

    {'banana': 6, 'cherry': 6}

Question 4

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 of elements in the resulting list.

  • C

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

  • D

    It only affects the readability of the code.

Question 5

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

  • A

    By converting a list to a tuple after using list comprehension.

  • B

    By using the tuple() constructor within the list comprehension.

  • C

    List comprehension can only create lists, not tuples.

  • D

    By enclosing each element in parentheses within the list comprehension.

Question 6

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 7

What is the purpose of the following list comprehension?

numbers = [1, -2, 3, -4, 5]
squared_positives = [x**2 if x > 0 else 0 for x in numbers]
 

  • A

    Squares all numbers.

  • B

    Squares only positive numbers.

  • C

    Squares only negative numbers.

  • D

    Squares numbers greater than 3.

Question 8

What is the purpose of the following set comprehension?

Python
words = ['python', 'list', 'comprehension']
unique_starting_letters = {word[0].upper() for word in words}


  • A

    Creates a list of unique starting letters of words.

  • B

    Creates a set of unique starting letters of words.

  • C

    Creates a dictionary with starting letters as keys and words as values.

  • D

    Counts the occurrences of each starting letter.

Question 9

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 10

Explain the term "nested list comprehension."

  • A

    It refers to using multiple conditions in a single list comprehension.

  • B

    It involves using a list comprehension inside another list comprehension.

  • C

    It is a type of list comprehension specific to nested lists.

  • D

    It is an advanced feature not related to list comprehensions.

There are 25 questions to complete.

Take a part in the ongoing discussion