Page 193 -
P. 193
comprehending data
Now that you know about list comprehensions, let’s write four of
them to process the coach’s four lists of timing values. Transform
each of your lists into sorted, sanitized version of themselves.
Grab your pencil and in the space provided, scribble the list
comprehensions you plan to use.
Q: So…let me get this straight: list comprehensions are good and list iterations are bad, right?
A: No, that’s not the best way to look at it. If you have to perform a transformation on every item in a list, using a list comprehension is the
way to go, especially when the transformation is easily specified on one line (or as a function chain). List iterations can do everything that list
comprehensions can, they just take more code, but iterations do provide more flexibility should you need it.
Geek Bits
Python’s list comprehension is an example of the language’s
support for functional programming concepts. There’s plenty of
debate about the best way to develop program code: either
procedurally, using functional programming techniques, or
using object orientation. At Head First Labs, we try not to get
involved in this debate, other than to rejoice in the fact that
Python supports, in one way or another, all three of these
programming practices.
you are here 4 157