How to return dictionary keys as a list in Python?

This will convert the dict_keys object to a list:

list(newdict.keys())

On the other hand, you should ask yourself whether or not it matters. It is Pythonic to assume duck typingif it looks like a duck and it quacks like a duck, it is a duck. The dict_keys object can be iterated over just like a list. For instance:

for key in newdict.keys():
    print(key)

Note that dict_keys doesn’t support insertion newdict[k] = v, though you may not need it.

Leave a Comment