How to get a list of all the keys from a Python dictionary?

PythonServer Side ProgrammingProgramming

To get a list of all keys from a dictionary, you can simply use the dict.keys() function. 

example

my_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'}
key_list = list(my_dict.keys())
print(key_list)

Output

This will give the output −

['name', 'time', 'location']

You can also get a list of all keys in a dictionary using a list comprehension. 

example

my_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'}
key_list = [key for key in my_dict]
print(key_list)

Output

This will give the output −

['name', 'time', 'location']
raja
Published on 05-Jan-2018 13:10:08
Advertisements