A sensor reports Celsius values that need Fahrenheit equivalents.
What You Need to Understand
Transforming a list means generating one new value for each original value. An explicit loop makes the steps visible before introducing a list comprehension.
When to Use It
A sensor reports Celsius values that need Fahrenheit equivalents.
Step-by-Step Python Example
celsius_values = [0, 20, 30]
fahrenheit_values = []
for celsius in celsius_values:
fahrenheit_values.append(celsius * 9 / 5 + 32)
print(fahrenheit_values)
How the Example Works
Each input contributes one transformed result; the output list has the same length and order as the input.
Example Output
[32.0, 68.0, 86.0]
[45.0, 90.0]
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
prices = [50, 100]
print([price * 0.9 for price in prices])
Important Behavior and Edge Cases
If the transformation changes units, name your variables to show which unit each value uses.
Try It Yourself
Convert a list of prices into prices with a 10 percent discount.
Common Beginner Mistakes
- If the transformation changes units, name your variables to show which unit each value uses.
- Overlooking how an iterable is consumed, how element positions are chosen, or whether a function creates a new list.
Key Takeaways
- Transforming a list means generating one new value for each original value.
- Each input contributes one transformed result; the output list has the same length and order as the input.
- If the transformation changes units, name your variables to show which unit each value uses.