runtime
127 / 500
question 127 Comprehensions · medium

Price labels without the loop

The team's style guide says a function that only builds a list from another list should be a comprehension. Refactor price_labels into a list comprehension. Behaviour must not change: free items still say Free, every other price shows the currency sign and two decimals, and there is still one label per price, in order.

Functionprice_labels(prices, currency) (same name, same result)
pricesa list of prices, 0 or more
Returnsone label per price: "Free" for 0, otherwise like "$12.50"
Structurethe result is built by a list comprehension, not by append in a loop
Examples
callprice_labels([12.5, 0, 7.49], '$')returns['$12.50', 'Free', '$7.49']
callprice_labels([31], '£')returns['£31.00']
callprice_labels([], '$')returns[]

Loading your progress…

Price labels without the loop — Python practice · Runtime