How to create an empty dictionary in Python?


The dictionary is the data structure available in python. Dictionaries are also known as associative memories or associative arrays. It is represented by curly braces {}. It stores the data in the form of key and value pairs.

Data from a dictionary can be accessed by its key, unlike other data structures which use indexes. To retrieve the value associated with a particular key, one must use index values. As keys in dictionaries are unique, any immutable object such as tuples or strings can be used to identify them. However, values stored in dictionaries do not need to be unique.

The empty dictionary can be created in two ways −

Using curly braces {}

One way to create an empty dictionary in python is using the curly braces {}. To do so, you demo need to assign the curly braces pair to a variable as shown in the following is the syntax −

Syntax

variable_name = {}

Where,

  • variable_name is the name of the dictionary

  • {} is the symbol for creating the empty dictionary

Example

In this example, we will assign the curly braces to the variable, then the dictionary will be created.

dict1 = {}
print("The dictionary created using curly braces ",dict1)
print(type(dict1))

Output

The following is the output, when creating the empty dictionary using the curly braces.

The dictionary created using curly braces {}
<class 'dict'>

Example

Here, we are trying to create an empty dictionary using { } and appending the values to the created empty dictionary.

dict1 = {}
print("The dictionary created using curly braces ",dict1)
dict1['a'] = 10
print("The dictionary after appending the key and value ",dict1)
print(type(dict1))

Output

The below is the output of creating an empty dictionary and appending values to it. We can see that the resulting structure is an empty dictionary, as indicated by its data type.

The dictionary created using curly braces  {}
The dictionary after appending the key and value  {'a': 10}
<class 'dict'>

Using the dict() function

We can also create the dictionary using the dict() function. Usually to create a dictionary using this function we need to pass the key-value pairs as parameters to it. But if we invoke this function without passing any parameters an empty dictionary is created.

Syntax

Following is the syntax for creating the empty dictionary.

variable_name = dict()

Where,

  • variable_name is the name of the dictionary

  • dict is the keyword for creating the empty dictionary

Example

Following is an example to create an empty dictionary using the dict() function −

dict1 = dict()
print("The dictionary created is: ",dict1)
print(type(dict1))

Output

The dictionary created is:  {}
<class 'dict'>

Example

In this example, initially we are creating an Empty dictionary and assigning values to it −

dict1 = dict()
print("Contents of the dictionary: ",dict1)
dict1['colors'] = ["Blue","Green","Red"]
print("Contents after inserting values: ",dict1)

Output

Contents of the dictionary:  {}
Contents after inserting values:  {'colors': ['Blue', 'Green', 'Red']}

Example

Let us see another example −

dict1 = dict()
print("Contents of the dictionary: ",dict1)
dict1['Language'] = ["Python","Java","C"]
dict1['Year'] = ['2000','2020','2001']
print("Contents after adding key-value pairs: ",dict1)

Output

Contents of the dictionary:  {}
Contents after adding key-value pairs:  {'Language': ['Python', 'Java', 'C'], 'Year': ['2000', '2020', '2001']}

Updated on: 15-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements