Hi @jaydacoder ,
Here's another approach, using a function,
inventory_itemizer, to return a string in the format exemlified in your original post ...
# create sorted list of containers
containers = inventory.keys()
inv_str = ""
# process each container
for key in containers:
inv_str += key + ":"
if type(inventory[key]) == list:
inv_str += " " + ", ".join(inventory[key])
inv_str += " " + str(inventory[key])
inv_str += "\n"
backpack: bedroll, bread loaf, xylophone
burlap bag: apple, small ruby, three-toed sloth
pocket: lint, seashell, strange berry
pouch: flint, gemstone, twine
For a creative programmer, there's still room for improvement. For example, the alignment of the items in the output can be refined.
EDIT (July 13, 2016) - The creative programmer may also want to investigate whether or not the function alters the original object that is passed to it in any manner, including the items it contains. If it is deemed necessary, a deep copy can be created within the function. This issue should be considered when working with certain mutable objects, such as
See Python: 8.17. copy — Shallow and deep copy operations.