How to Create a List of Keys in Python?

Estimated read time 1 min read

To create a list of keys in Python, you can use the keys() method on a dictionary object. Here’s an example:

my_dict = {'a': 1, 'b': 2, 'c': 3}
key_list = list(my_dict.keys())
print(key_list)

Output:

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

In this example, we define a dictionary variable my_dict containing three key-value pairs. We then call the keys() method on my_dict to obtain a list of the dictionary keys. Finally, we convert this list of keys into a Python list using the list() function and print the resulting key_list variable.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply