get all keys of dictionary in python by R4R Team

- keys() is a python dictionary function which return all the keys of the dictionary.

Syntax-

dict.keys()

Example

dictionary is d={1:'a',2:'b',3:'c'}
d.keys() will return 1,2,3 //keys of d


program-

d=dict()
print("Enter key 1")
n=input()
d.fromkeys(n)
print("Enter value of key 1")
d[n]=input()

print("Enter key 2")
n=input()
d.fromkeys(n)
print("Enter value of key 2")
d[n]=input()

print("Enter key 3")
n=input()
d.fromkeys(n)
print("Enter value of key 3")
d[n]=input()

print("Dictionary is ",d)
print("All keys of dictionary is",d.keys())
print("All keys of dictionary is",*d.keys())


output-

Enter key 1
1
Enter value of key 1
one
Enter key 2
2
Enter value of key 2
two
Enter key 3
3
Enter value of key 3
three
Dictionary is {'1': 'one', '2': 'two', '3': 'three'}
All keys of dictionary is dict_keys(['1', '2', '3'])
All keys of dictionary is 1 2 3


-In this program, we take a input of keys and their values and make a dictionary which is {'1': 'one', '2': 'two', '3': 'three'} then using d.keys() we will get dict_keys(['1', '2', '3']), then by *d.keys() will give 1 2 3




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!