Assessment Questions for List Comprehension Concept Map

May 14, 2014 • Scott Burns

These questions assess a students understanding of the list comprehension concept map.

  1. Which of the following statements about list comprehensions is correct?
  • A list comprehension produces a new list only when the conditional statement is True for all elements of the container.
  • List comprehensions require a conditional statement.
  • The body of a list comprehension must include only one expression.
  • If none of the elements of the container meet the conditional statement, the list comprehension creates a list of None equal in length of the container.
  1. Given the following:
def square(z):
    return z * z

def is_number(z):
    from numbers import Number
    return isinstance(z, Number)

items = [1, '4', 9, 16.1, [1, 2, 3]]

Write a list comprehension that squares only the numeric elements of items.