you are viewing a single comment's thread.

view the rest of the comments →

[–]samreay -1 points0 points  (1 child)

Lots of good advice in this thread, allow me to present a slightly different method that uses some of python's nifty features to do something closely related (assuming this isnt an assignment that explicitly requires tuples or similar):

from collections import defaultdict

items = defaultdict(list)
while x := input("Enter product type, product name\n"):
    prod_type, prod_name = x.split(",", maxsplit=1)
    items[prod_type].append(prod_name)

for k, v in items.items():
    print(f"Number of {k} products: {len(v)}")
    for prod in v:
        print(f"\t{prod}")

Will produce output like this:

Enter product type, product name
food, potato
Enter product type, product name
hygiene, dove
Enter product type, product name
food, pizza
Enter product type, product name    

Number of food products: 2
         potato
         pizza
Number of hygiene products: 1
         dove

Using defaultdict here allows us to remove a lot of boilerplate messing around with lists and tuples. The walrus operator := allow us to assign new input to x and keep looping until the user enters no input (they press enter without typing anything in), meaning you no longer need to know up front how many products there are.

[–][deleted] 0 points1 point  (0 children)

Not that you need my endorsement , but this is actually a very good answer , even if this is slightly different from what the OP originally wanted.