World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
How can I retrieve a list of dictionary keys in Python?
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()) print(keys_list)
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.