Type Conversion Functions
List comprehension creates a new sequence from an existing one by applying a transformation to each item in a single, compact line.
From Existing Data to New Data
Often, data begins in one form but needs to be transformed into another. For example, a list may contain numbers represented as strings, while a calculation requires actual integers. A list comprehension creates a new sequence from an existing sequence by applying a transformation to each item in one compact line.
The central idea is item-by-item transformation: take an item from the source sequence, apply the expression to it, and place the resulting value in the new sequence.
The Three-Part Pattern
A list comprehension has three key parts: an expression that describes what to compute, a variable that represents the current item, and an iterable that provides the source data.
The expression comes first. The for clause then introduces the variable, and the iterable identifies the source sequence. The square brackets enclose the complete list-building operation.
Tracing a Conversion
Suppose the source data is a list of string values representing numbers. The goal is to create a new list containing integer values so that the integers can be summed. Each source string is assigned to x, int(x) converts that current item, and the converted result is added to the new list.
60Execution follows the same path for every item. The current item is bound to x, int(x) is evaluated using that item, and the result becomes the next element of the new list. Repeating this process builds the final sequence one element at a time.
Mapping a Loop to a Comprehension
The traditional version makes each operation explicit: initialize an empty list, iterate through the source, transform the current item, and append the result. The comprehension combines those initialization, iteration, transformation, and appending steps into one expression.
Mistakes in Comprehension Syntax
Forgetting the square brackets
The list-building operation is not enclosed in the required brackets.
Fix:
[int(x) for x in values]Using the wrong variable name
The for clause binds the current item to x, but the expression refers to value.
Fix:
[int(x) for x in values]Placing the for clause before the expression
The expression must come first in the comprehension.
Fix:
[int(x) for x in values]
When debugging a comprehension, identify its three parts in order: first the expression, then the variable introduced by for, and finally the iterable after in. This makes it easier to check whether the current item is being transformed and whether the source sequence is the intended one.
Practice the Pattern
Write a list comprehension that creates a new list by converting every item in a list called values into an integer. Then label the expression, variable, and iterable.
Hints
- The expression should perform the integer conversion.
- Use a variable for the current item.
- Use values as the iterable.
- Place the expression before the for clause.
What do you think happens?
Which list comprehension is equivalent to a loop that appends int(x) for every x in values?
Reveal answer
Answer: [int(x) for x in values]
int(x) is the expression, x is the variable for the current item, and values is the iterable. The expression appears before the for clause.
Key Takeaways
- A list comprehension creates a new sequence by transforming items from an existing sequence.
- Its three key parts are the expression, variable, and iterable.
- The expression comes first, followed by the for clause, variable, and iterable.
- A list comprehension can express the same simple transformation as a for loop with append().
- Use a traditional loop when the transformation requires multiple steps or becomes complex.
Key Takeaways
- List comprehensions create a new sequence from an existing sequence by transforming each item.
- The syntax contains an expression, a variable, and an iterable.
- The expression is written before the for clause.
- A traditional loop with append() maps directly to the parts of a comprehension.
- Comprehensions are concise for simple transformations, while traditional loops may be clearer for complex logic.