Categories
Python Answers

How to return dictionary keys as a list in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *