I am learning programming in python 3 and I am having a issue with formating 2 for statements inside a list. Here is how I did it :
items = {
"Iphone X": {"price": 1000, "stock":10},
"Samsung S9": {"price": 800, "stock":10},
"Huawei P20": {"price": 600, "stock":10},
"HTC Vive": {"price": 400, "stock":10}
}
test = ["{} - {}$".format(nume, pret) for nume in items.keys() for pret in items[nume]["price"]]
print("\n".join(test))
I don't know why when I change the type of items[nume]["price"]
into str
it prints the key + the value , it prints it as many digits i have in my value.
If I don't change the type of items[nume]["price]
it tells me TypeError: 'int' object is not iterable
.