-1

So I have this inventory dict and an empty items list that I want to fill it with the keys in the inventory dict. Any clues on how do I fill the list?

# Inventory Dict
inventory = {}
inventory["item a"] = 12
inventory["item b"] = 10
inventory["item x"] = 25
print(inventory)

# Item List
items = []
J K
  • 1
  • 3
  • 2
    `items = list(inventory.keys())` – joel Jul 16 '18 at 18:49
  • have a read of https://docs.python.org/3/library/stdtypes.html#typesmapping – joel Jul 16 '18 at 18:51
  • How if I want to in a loop that fills the list with dict's keys? – J K Jul 16 '18 at 18:52
  • 1
    probably duplicate of https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops/3294899#3294899 – joel Jul 16 '18 at 18:59
  • Possible duplicate of [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) – divibisan Jul 16 '18 at 21:19

1 Answers1

1
items = list(inventory.keys())

or in a loop (as requested)

for key in inventory:
    items.append(key)

though as is there's no need for the loop. Perhaps there would be in another situation

joel
  • 6,359
  • 2
  • 30
  • 55