All Courses

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

By Manerushi149@gmail.com, a month ago
  • Bookmark
0

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

Python syntax
Dictionary's
Converting a dictionary's
Dictionary's keys
List
1 Answer
0
Goutamp777

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.

Your Answer

Webinars

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