fromkeys() dictionary function in python by R4R Team

- fromkeys() is a python function which are used to insert a new keys in the dictionary.

Syntax-

dict.fromkeys(key)

Example

dictionary is d={1:"one",2:"two"}
then d.fromkeys(3) will give {1:"one",2:"two",3:None}


program-

print("We store data of three keys")
d=dict()
print("Enter first key")
k=input()
d.fromkeys(k)
print("Enter value for first key")
d[k]=input()

print("Enter second key")
k=input()
d.fromkeys(k)
print("Enter value for second key")
d[k]=input()

print("Enter third key")
k=input()
d.fromkeys(k)
print("Enter value for third key")
d[k]=input()

print("Dictionary is ")
print(d)


output-

We store data of three keys
Enter first key
1
Enter value for first key
one
Enter second key
2
Enter value for second key
two
Enter third key
3
Enter value for third key
three
{'1': 'one', '2': 'two', '3': 'three'}


-In this program, initially we have a empty dictionary by d=dict(), then we generate a key by fromkeys() then we insert the value and print the dictionary.




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!