Python list comprehension

¡Supera tus tareas y exámenes ahora con Quizwiz!

What built in function is list comprehension similar to?

.map()

What is the purpose of list comprehension?

To create subsets or new lists in a terse and efficient manner while still remaining readable

What would the results of either of these statements look like?

[0, 1, 2, 0, 1, 2, 0, 1, 2]

What is the simplest syntax for a list comprehension statement?

[expression for expression1 in iterable [if condition]]

What would be the list comprehension syntax for squaring the integers from 0-5?

[x**2 for x in range(5)]

How would you convert the following nested for loop to list comprehension: res = [] for x in range(3): for y in range(3): res.append(y)

[y for x in range(3) for y in range(3)]

Define each of the elements in the last answer

expression is the list to be acted on expression1 is the item used for iteration iterable is any iterable item [if condition] defines an optional if condition to filter results


Conjuntos de estudio relacionados

gravity, eclipses and tides review

View Set

CHM212 Chapter 21 Learning Curve

View Set