Start learning free

LESSON 272 OF 673 · TUPLES, SORTING & BUILT-INS

Price List in Python with enumerate and zip

Practice Read the full explanation

You get a list of strings items and a list of integers prices. Return a list of strings with one line per item.

The price at each index belongs to the item at the same index.

A line looks like "1. apple: 3". It has the item's number, then the item, then its price.

The numbers start at 1. The lines keep the order of items.

 

Example 1:

Input: items = ["apple", "bread", "milk"], prices = [3, 2, 4]
Output: ["1. apple: 3", "2. bread: 2", "3. milk: 4"]
Explanation: The apple is first and costs 3.
The bread is second and costs 2.
The milk is third and costs 4.

Example 2:

Input: items = ["tea"], prices = [5]
Output: ["1. tea: 5"]
Explanation: One item gives one line, number 1.

Example 3:

Input: items = ["pen", "pen", "ink"], prices = [2, 3, 0]
Output: ["1. pen: 2", "2. pen: 3", "3. ink: 0"]
Explanation: The shop sells two kinds of pen.
Each one gets its own line, number and price.
The ink is free, so its price is 0.

 

Constraints:

  • items and prices have the same length.
  • They have 1 to 100 entries.
  • Each item is one word of lowercase letters.
  • Each price is a whole number from 0 to 1000.
STEP 1 OF 4
    Ctrl + Enter to check

    PYTHON · TUPLES, SORTING & BUILT-INS

    Price List in Python, explained

    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

    1. Start an empty list lines.
    2. Loop over enumerate(zip(items, prices), start=1), naming the parts number and (item, price). Keep the parentheses around (item, price): each step has a number and a pair, not three values.
    3. Append an f-string f"{number}. {item}: {price}" to lines. For the first pair of Example 1 it gives 1. apple: 3.
    4. 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 with ValueError: not enough values to unpack (expected 3, got 2).
    • Leaving out start=1, which makes the first line 0. 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.
    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free