Skip to content Skip to sidebar Skip to footer

Python: Getting Keys Of A Dictionary

When I do dict.keys() in python3, I do not get a list, but an object of class dict_keys. Why is this and what can I do with this object? How to get the list? Example code: type(dic

Solution 1:

dict.keys returns a dict_keys object, which is an iterable object.

So, you can either convert it to a list using:

keys = list(dict.keys())

Or, you can simply iterate over the dict_keys object, like it was intended:

for key indict.keys():
    print(key)

In your example, it will print out:

sape
guido
jack

Post a Comment for "Python: Getting Keys Of A Dictionary"