Sometimes, we want to return dictionary keys as a list in Python.
In this article, we’ll look at how to return dictionary keys as a list in Python.
How to return dictionary keys as a list in Python?
To return dictionary keys as a list in Python, we can use the dict’s keys
method.
For instance, we write
for key in newdict.keys():
print(key)
to call newdict.keys
to return an iterator with the keys of the newdict
dictionary.
Then we use a for loop to loop through the keys.
Conclusion
To return dictionary keys as a list in Python, we can use the dict’s keys
method.