Вы должны проверить свои фактические списки, а не список ключей () - представление, которое вы получаете из dict:
class Inventory:
def __init__(self):
self.dict_inv = dict()
self.count_inv = int(input("Enter the number of inventories: "))
for count in range(self.count_inv):
name_inv = str(input("Enter Inventory #%d: " % (count+1)))
# simply add the list here
self.dict_inv[name_inv] = []
sample_add = str(input("Add item here: "))
# simply add empty list for that key directly, no need to store count here
self.dict_inv[name_inv].append(sample_add)
sample_add2 = str(input("Add another item here: "))
# simply append to the list here
self.dict_inv[name_inv].append(sample_add2)
for key in self.dict_inv.keys():
# dont create a list of one single key, use the dicts value instead
if type(self.dict_inv[key]) is list:
print("{} is list".format(key) )
print("items in {} are {}".format(key, self.dict_inv[key]))
Inventory()
Вывод для ввода 2,egg,1,2,tomato,3,4
:
egg is list
items in egg are ['1', '2']
tomato is list
items in tomato are ['3', '4']
Измените выход, используя:
print("items in {} are {}".format(key, ', '.join(self.dict_inv[key])))
, чтобы приблизиться к желаемому выходу:
egg is list
items in egg are 1, 2
tomato is list
items in tomato are 3, 4
HTH