Sobes.tech
Junior — Middle

How can you get a list of all keys from a dictionary in Python?

sobes.tech AI

Answer from AI

In Python, you can get a list of all dictionary keys using the .keys() method. It returns a view object of the keys, which can be converted into a list if needed:

my_dict = {'a': 1, 'b': 2, 'c': 3}
keys = list(my_dict.keys())
print(keys)  # Outputs: ['a', 'b', 'c']

You can also simply iterate over the dictionary, as by default it iterates over the keys.