Python List Comprehension Quiz

Last Updated :
Discuss
Comments

Question 1

In list comprehension, what does the expression represent?

  • The iterable being traversed.

  • The condition for filtering elements.

  • The value to be included in the new list.

  • The index of the current element.

Question 2

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

  • When the code needs to be more concise.

  • When the logic involves complex conditions.

  • In situations where performance is critical.

  • List comprehension is always preferable.

Question 3

What will the following list comprehension output?

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

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

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

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

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

Question 4

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

  • The order has no effect on the result.

  • It determines the order of elements in the resulting list.

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

  • It only affects the readability of the code.

Question 5

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

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

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

  • List comprehension can only create lists, not tuples.

  • By enclosing each element in parentheses within the list comprehension.

Question 6

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

  • No, it can only be used with lists.

  • Yes, it can be used with any iterable.

  • Yes, but only with strings.

  • 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]
 

  • Squares all numbers.

  • Squares only positive numbers.

  • Squares only negative numbers.

  • Squares numbers greater than 3.

Question 8

What is the purpose of the following set comprehension?

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

  • Creates a list of unique starting letters of words.

  • Creates a set of unique starting letters of words.

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

  • Counts the occurrences of each starting letter.

Question 9

Explain the concept of filtering in list comprehension.

  • It involves transforming elements based on a specified condition.

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

  • It is a technique for optimizing list comprehension.

  • It is not applicable to list comprehension.

Question 10

Explain the term "nested list comprehension."

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

  • It involves using a list comprehension inside another list comprehension.

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

  • It is an advanced feature not related to list comprehensions.

There are 25 questions to complete.

Take a part in the ongoing discussion