Once upon a time, in a small town filled with the aroma of freshly baked goods, there lived two friends: BuchuPaku and KolkePaku. BuchuPaku ran a cozy little bakery where he spent his days whipping up delicious cakes and cupcakes. He loved baking but he was teaching himself Python. He wanted to automate some of his work, hoping it would make life easier. But one thing drove him nuts—writing long, tedious code.
One sunny afternoon, KolkePaku dropped by the bakery. As he peeked over BuchuPaku’s shoulder at his laptop screen, he chuckled. “Buchu da, why are you writing so much code? It looks like you’re trying to bake an entire cake just to frost a cupcake!”
BuchuPaku sighed. “Well, I’m applying discounts to my cupcake prices using a for loop. What else am I supposed to do?”
KolkePaku smirked. “Ever heard of list comprehension ? Trust me, it’s your new best friend.”
BuchuPaku raised an eyebrow. “List comprehension? Sounds fancy. What does it even mean? Show me.”
The List of Cupcakes
BuchuPaku had a list of cupcake prices:
prices = [5, 10, 15, 20]
He wanted to apply a 10% discount on all cupcakes. He wrote a long loop:
discounted_prices = []
for price in prices:
discounted_prices.append(price * 0.9)
“Ah Too long! Buchu da” KolkePaku said. “Put the for loop in the list!”
discounted_prices = [price * 0.9 for price in prices]
BuchuPaku’s eyes widened. “That’s short! And works the same.“
Step by step
- Input List (
prices
) :- The variable
prices
is a list of numerical values (e.g.,[5, 10, 15, 20]
). Each value in the list represents the original price of an item.
- The variable
- Iteration (
for price in prices
) :- The
for price in prices
part iterates over each element in theprices
list. For every iteration, the current element is assigned to the variableprice
.
- The
- Operation (
price * 0.9
) :- For each
price
in the list, the code calculates a discounted price by multiplying the original price by0.9
. This effectively applies a 10% discount (since0.9
means 90% of the original price).
- For each
- List Comprehension (
[...]
) :- The square brackets
[]
indicate that the result of the operation (price * 0.9
) will be collected into a new list.
- The square brackets
- Output List (
discounted_prices
) :- The final result is a new list called
discounted_prices
, which contains the discounted values for each price in the originalprices
list.
- The final result is a new list called
A Sweet Ending
With his newfound knowledge of list comprehension, BuchuPaku cleaned up his bakery code in no time. Not only did he save hours of typing, but he also reduced the chances of making mistakes.
“Python really is fun!” BuchuPaku exclaimed, feeling like a tech-savvy wizard. “I think I’ll call list comprehension my secret ingredient from now on.”
KolkePaku nodded approvingly. “Fast and sweet—just like your cupcakes.”
And so, armed with his baking skills and Python tricks, BuchuPaku continued to run his bakery more efficiently than ever.
Now it’s your turn! Go ahead and try out list comprehension in your own Python projects. Who knows? Maybe you’ll find it just as magical as BuchuPaku did.