Write multiple ways of creating dictionary with example
Answers
Answered by
46
Answer:
To create a Dictionary, use {} curly brackets to construct the dictionary and [] square brackets to index it. Separate the key and value with colons : and with commas , between each pair. As with lists we can print out the dictionary by printing the reference to it.
Answered by
1
Answer:
1. Defining it yourself:
a) _____________
dict={1: "one", 2:"Two"}
______________
b) ______________
dict={ 1="one", 2="Two"}
______________
2. Asking user to define it:
______________________
n=int(input("Enter the number of pairs: "))
dict={}
for i in range(0, n):
dict[i]= input("Enter a value: ")
print(dict)
_________________________
3. Using built in functions:
__________________________
li = [1,2,3,4]
w = dict.fromkeys(li,"something" )
print(w)
Similar questions