I wanted to use ".join" to display the items in my list, each in a different line. I'm working with a nested list, so it doesn't really work the way I'm doing it. I'm a beginner at pyhton and so I was wondering if there actually is a way to solve this.
It shows "sequence item 0: expected str instance, list found" on line 10. How can I make this work?
product = str(input("What is the product? "))
quantity = str(input("What is the quantity? "))
sales = []
while product != "" and quantity != "":
sales.append([product, quantity])
product = str(input("What is the product? "))
quantity = str(input("What is the quantity? "))
list = "\n".join(sales)
print(list)
This is just an exercise I found on youtube, I know it's pretty weird.
You want to loop through the list, formatting the inner lists, then join the result.
list = "\n".join(f"{a}: {b}" for a,b in sales)
That code took values in sales
(the inner list), unpacked them to a
and b
, used an f-string to format them and then emitted them for the "\n".join()
operation.