Problem overview
The method priceList gets a list of strings items and a list of integers prices of the same length. The price at each index belongs to the item at the same index. It returns a list of strings, one line per item, like "1. apple: 3": the item's number, a dot and a space, the item, a colon and a space, and the price. Numbers start at 1, and the lines keep the order of items.
For items = ["apple", "bread", "milk"] and prices = [3, 2, 4] the answer is ["1. apple: 3", "2. bread: 2", "3. milk: 4"].
Approach
Two things are needed for each line: the item with its price, and a count. zip(items, prices) pairs each item with the price at the same position. enumerate(..., start=1) adds a count that begins at 1. Together they give three pieces per step, in the shape (number, (item, price)): a number and a pair.
No index variable is needed, so there is no range(len(items)) and no items[i].
Step by step
- Start an empty list
lines. - Loop over
enumerate(zip(items, prices), start=1), naming the partsnumberand(item, price). Keep the parentheses around(item, price): each step has a number and a pair, not three values. - Append an f-string
f"{number}. {item}: {price}"tolines. For the first pair of Example 1 it gives1. apple: 3. - After the loop, return
lines.
The solution in Python
class Solution:
def priceList(self, items: List[str], prices: List[int]) -> List[str]:
lines = []
for number, (item, price) in enumerate(zip(items, prices), start=1):
lines.append(f"{number}. {item}: {price}")
return lines
Inside an f-string, each name in braces is replaced by its value, and integers such as number and price are turned into text for you. The text between the braces, the dot, the space and the colon, is copied as it is.
The order of the two lists in zip sets the order inside each pair. zip(items, prices) gives pairs such as ('pen', 2), and unpacking them as (item, price) matches that. With zip(prices, items) the pairs would be (2, 'pen'), and the names would hold the wrong values.
The same lines can also be built with a list comprehension: [f"{n}. {i}: {p}" for n, (i, p) in enumerate(zip(items, prices), start=1)]. For ["pen", "ink"] and [2, 0] it gives ['1. pen: 2', '2. ink: 0'], the same as the loop. Both are fine; the loop with append is easier to read while you are learning.
Edge cases
The two lists have the same length, so zip never drops an item here. One item gives one line, numbered 1. A price of 0 is a normal price and prints as 0, and two items with the same name are not merged: each position gets its own line. The numbers come from enumerate, not from the item or the price, so duplicate items don't confuse them.
Testing
["apple", "bread", "milk"], [3, 2, 4] -> ['1. apple: 3', '2. bread: 2', '3. milk: 4']
["tea"], [5] -> ['1. tea: 5']
["pen", "pen", "ink"], [2, 3, 0] -> ['1. pen: 2', '2. pen: 3', '3. ink: 0']
The third input has a repeated item: each pen gets its own line, number and price. The price 0 is printed as 0.
Common mistakes
- Writing
for number, item, price in enumerate(zip(...)). Each step has two parts, so Python stops withValueError: not enough values to unpack (expected 3, got 2). - Leaving out
start=1, which makes the first line0. apple: 3. - Swapping the order in
zip, which puts each price where the item belongs. - Joining the lines into one string. The method returns a list of strings, one per item.
