All Courses

What is the Python syntax for converting a dictionary's keys into a list?

By, a month ago
  • Bookmark

How can I retrieve a list of dictionary keys in Python?   

Python syntax
Converting a dictionary's
Dictionary's keys
1 Answer

To convert the keys of a dictionary into a list in Python, you can use the keys() method of the dictionary and then convert the returned object to a list using the list() function. Here's an example:

my_dict = {'a': 1, 'b': 2, 'c': 3}

# convert keys to a list
keys_list = list(my_dict.keys())


This code will output:

['a', 'b', 'c']

In Python 3.7 and later versions, the order of the keys in the list will be the same as the order in which they were inserted into the dictionary. In earlier versions of Python, the order may be arbitrary.

Your Answer


Live Masterclass on : "How Machine Get Trained in Machine Learning?"

Mar 30th (7:00 PM) 516 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More