Google News
logo
Python Program to Print all key names in a dictionary, one by one
In the following example of Python program You can use a for loop to iterate over the keys of a dictionary and print them one by one :
Program :
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

for key in my_dict:
    print(key)
Output :
name
age
city
Alternatively, you can use the `keys()` method to get a list of all the keys in the dictionary and then iterate over the list :
Program :
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

for key in my_dict.keys():
    print(key)
Output :
name
age
city
Both of these programs produce the same output.