xxxxxxxxxx
# define a dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
# get the keys of the dictionary
keys = my_dict.keys()
# print the keys
print(keys) #print as dict_keys(['a', 'b', 'c'])
# get a list of the keys
keys = list(my_dict.keys())
# print the keys
print(keys) #print as ['a', 'b', 'c']
xxxxxxxxxx
# To get all the keys of a dictionary use 'keys()'
newdict = {1:0, 2:0, 3:0}
newdict.keys()
# Output:
# dict_keys([1, 2, 3])
xxxxxxxxxx
# Python program to get
# dictionary keys as list
def getList(dict):
list = []
for key in dict.keys():
list.append(key)
return list
# Driver program
dict = {1:'Geeks', 2:'for', 3:'geeks'}
print(getList(dict))
xxxxxxxxxx
# Define a dictionary
my_dict = {'key1': 1, 'key2': 2, 'key3': 3}
# Retrieving dictionary keys
keys = my_dict.keys()
# Printing the keys
for key in keys:
print(key)
xxxxxxxxxx
mydict = {1: 'Geeks', 2: 'for', 3: 'geeks'}
keysList = list(mydict.keys())