1

I'm having trouble trying to figure out how to print out just only the key from a nested dictionary. For example, I want to print out just only 100 and 111.


db = {  100: {"fruit": "orange",
              "dairy": "milk",
              "meat": "steak"},
        111: {"fruit": "apple",
              "dairy": "cheese",
              "meat": "chicken"}
        }
t l n
  • 25
  • 4
  • 2
    Does this answer your question? [How to print a dictionary's key?](https://stackoverflow.com/questions/5904969/how-to-print-a-dictionarys-key) – xandermonkey May 11 '20 at 21:45
  • The fact that the values are also dictionaries, is completely irrelevant. You print out the keys of `db` the same way that you print out the keys of any other dictionary, so this is a duplicate. – Karl Knechtel May 11 '20 at 21:57

3 Answers3

1

You can use keys() function to return only keys from a dictionary.

print(db.keys())

Additionaly, to get values from the dictionary, "values()" function can be used.

print(db.values())
Anshul Vyas
  • 633
  • 9
  • 19
0

print(db.keys()) should do what you need

John Rice
  • 1,737
  • 8
  • 14
0

You can just iterate that'll give the keys

for k in db:
    print(k)
Zabir Al Nazi
  • 10,298
  • 4
  • 33
  • 60