Related Articles

Related Articles

Initialize an Empty Dictionary in Python
  • Last Updated : 13 Nov, 2020

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized. 

Now, Let’s see the different ways to create an Empty Dictionary.

Method 1: Use of { } symbol.

We can create an empty dictionary object by giving no elements in curly brackets in the assignment statement

Code:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate use of
# {} symbol to initialize dictionary
emptyDict = {}
 
# print dictionary
print(emptyDict)
 
# print length of dictionary
print("Length:", len(emptyDict))
 
# print type
print(type(emptyDict))

chevron_right


Output

{}
Length: 0
<class 'dict'>




Method 2: Use of dict() built-in function.

Empty dictionary is also created by dict() built-in function without any arguments.

Code:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate use of 
# dict() built-in function to
# initialize dictionary
emptyDict = dict()
 
# print dictionary
print(emptyDict)
 
# print length of dictionary
print("Length:",len(emptyDict))
 
# print type
print(type(emptyDict))

chevron_right


Output

{}
Length: 0
<class 'dict'>




Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :