belongs to collection: Python Dictionary Programs
In Python programming language, a dictionary is a collection of an unordered collection of data values in the form of key-value pair.
An empty dictionary can be created by using the curly braces ({}) without assigning any values.
Syntax:
dictionary_name = {}
Program:
# Python program to create an empty dictionary # creating an empty dictionary dict_a = {} # printing the dictionary print("dict_a :", dict_a) # printing the length print("Total elements: ", len(dict_a))
Output:
dict_a : {} Total elements: 0
The dict() method is used to create a dictionary, it can also be used to create an empty dictionary.
dictionary_name = dict()
# Python program to create an empty dictionary # creating an empty dictionary dict_a = dict() # printing the dictionary print("dict_a :", dict_a) # printing the length print("Total elements: ", len(dict_a))
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
1) Creating an empty dictionary using {}
An empty dictionary can be created by using the curly braces ({}) without assigning any values.
Syntax:
Program:
Output:
2) Creating an empty dictionary using the dict() method
The dict() method is used to create a dictionary, it can also be used to create an empty dictionary.
Syntax:
Program:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer