You are currently viewing How to Create Python Empty Dictionary?

How to create an empty Python dictionary? You can create an empty dictionary by using curly braces {} or the built-in dict() constructor. You can use some of the operators and functions to check if a dictionary is empty or not.

Advertisements

In this article, I will explain how to create an empty dictionary using curly braces({}), dict() constructer, and keys of dictionary with examples.

1. Following are Quick Examples of Creating an Empty Dictionary

If you are in a hurry, the following are quick examples of creating an empty dictionary.


# Quick examples of create empty dictionary

# Example 1: Create an empty dictionary using {} curly braces
my_dict = {}
print("Empty dictionary:", my_dict)

# Example 2: Create an empty dictionary using dict()
my_dict = dict()
print("Empty dictionary:", my_dict)

# Example 3: Initialize the keys of dict
dict_keys = ["course", "fee", "duration"]
# Create empty dictionary using keys of dict
empty_dict = dict(zip(dict_keys, [None]*len(dict_keys)))

# Example 4:  Create an empty nested dictionary
my_dict = {'' : {}, '':{}}
print("Empty nested dictionary:", my_dict)

# Example 5: Check if the dictionary is empty using bool() method
my_dict = {"course": "Python", "fee": 4000, "duration": "45 days"}
empty_dict = {}
print("If the dictionary is empty:", bool(empty_dict))
print("If the dictionary is not empty:", bool(dict))

# Example 6: Check if the dictionary is empty using if statement
empty_dict = {}
if my_dict:
    print("Dictionary is not empty")
else:
    print("Dictionary is empty")
   
# Example 7: Check if the dictionary is empty using not operator
empty_dict = {}
print("Dictionary is empty:", not empty_dict)

# Example 8: Check if the dictionary is empty using len() method
# my_dict = {"course": "Python", "fee": 4000, "duration": "45 days"}
empty_dict = {}
dict = len(empty_dict)

if dict == 0:
    print ("Dictionary is empty")
else:
    print ("Dictionary is not empty")

What is Python Dictionary

A Python dictionary is a collection that is unordered, mutable and does not allow duplicates. Each element in the dictionary is in the form of key:value pairs. Dictionary elements should be enclosed with {} and key: value pair separated by commas. The dictionaries are indexed by keys.

Moreover, dictionaries are mutable data types, which means that you can be added, deleted, and updated their key:value pairs.

A value can be any data type such as a number(int, float), a string, a list, a tuple, or even another dictionary and it allows duplicates. On the other hand, keys are immutable types such as string, numbers(int, float), and tuple and it doesn’t allow duplicates. In case, you add duplicates, which will update the existing keys of the Python dictionary.

2. Syntax of Dictionary

A dictionary is a collection of key-value pairs enclosed with {}, each key and its corresponding value separated by a colon (:).


# Syntax of the dictionary
courses = { "course": "python",
         "fee": 4000,
         "duration": "30 days"}

3. Create an Empty Dictionary in Python using Curly Braces({})

Python dictionaries can be created using curly braces. you can use curly braces to create both empty dictionaries and dictionaries with key/value pairs. This is the easiest way to create an empty dictionary. Let’s pass no argument to curly braces to get the empty dictionary.


# Create an empty dictionary using {} curly braces
my_dict = {}
print("Empty dictionary:", my_dict)
print(type(my_dict))

Yields below output.

Python empty dictionary

4. Using dict() Method to Create Empty Dictionary

Another easy way of creating an empty dictionary using the dict()constructer method which is a built-in function of Python. You can use this method to create empty dictionaries and dictionaries with key/value pairs. Let’s pass no argument to the dict() method to get the empty dictionary.


# Create an empty dictionary using dict()
my_dict = dict()
print("Empty dictionary:", my_dict)
print(type(my_dict))

5. Using keys of Dict Create Empty Dictionary

You can use the zip() and len() methods to create an empty dictionary with keys and no values. Python zip() is a built-in function that is used to zip or combine the elements from two or more iterable objects like lists, tuples, dictionaries, etc. In this example, You can use the zip() function to combine the dict_keys and [None]*len(dict_keys) and get the empty dictionary after converting the zip object to the dictionary.


# Initialize the keys of dict
dict_keys = ["course", "fee", "duration"]
# Create empty dictionary using keys of dict
empty_dict = dict(zip(dict_keys, [None]*len(dict_keys)))
print("Empty dictionary:", empty_dict)

Yields below output.

Python empty dictionary

As you can see from the above, an empty dictionary has been returned with keys and no values.

6. Create an Empty Nested Python Dictionary

You can also use the dict() method to create an empty nested dictionary. You can use this method to create empty nested dictionaries and nested dictionaries with dictionaries. Let’s pass the empty dictionary in to dict() method to get the empty nested dictionary.

For example:


# Create an empty nested dictionary
my_dict = {'' : {}, '':{}}
print("Empty nested dictionary:", my_dict)
print(type(my_dict))

# Output:
# Empty nested dictionary: {'': {}}

7. Check If the Python Dictionary is Empty

Following are the various ways to check if the dictionary is empty,

  • bool() method
  • If statement
  • Not operator
  • len() method

7.3 Check If the Python Dictionary is Empty using bool()

You can use the bool() function to check if the dictionary is empty or not. This function is used to convert value to boolean value. It takes a single argument and returns either True or False. If the passed argument is an empty dictionary it will return False or not, it will return True.


# Check if the dictionary is empty using bool() method
my_dict = {"course": "Python", "fee": 4000, "duration": "45 days"}
empty_dict = {}
print("Empty dictionary:", empty_dict)
print("If the dictionary is empty:", bool(empty_dict))
print("Given dictionary:", my_dict)
print("If the dictionary is not empty:", bool(my_dict))

Yields below output.

Check if Python dictionary is empty

7.4 Use the If statement to Check if the Dictionary is Empty

In Python if statement is used to check if the dictionary is empty or not. If the dictionary is empty if part will be executed otherwise else part will be executed.

In this example, I will create two dictionaries one is an empty dictionary and another one is a dictionary with key/value pair. For the first time, you can apply if condition on my_dict and second time apply on empty_dict.


# Check if the dictionary is empty using if statement
my_dict = {"course": "Python", "fee": 4000, "duration": "45 days"}
empty_dict = {}
print("Given dictionary:", my_dict)
if my_dict:
    print("Dictionary is not empty")
else:
    print("Dictionary is empty")
print("Empty dictionary:", empty_dict)   
if empty_dict:
    print("Dictionary is not empty")
else:
    print("Dictionary is empty")

Yields below output.

Check if Python dictionary is empty

7.5 To Check If the Dictionary is Empty using not Operator

You can use the not operator over the empty dictionary and check if it is empty or not. This boolean operator takes a single operand and returns the boolean value True if the operand is False and vice versa.

The boolean value of the empty dictionary is evaluated as False when we apply the not operator over the empty dictionary, which returns a negation of False that is True.


# Check if the dictionary is empty using not operator
empty_dict = {}
print("Given dictionary:", empty_dict)
print("Dictionary is empty:", not empty_dict)

# Output:
# Given dictionary: {}
# Dictionary is empty or not: True

7.6 To Check If the Dictionary is Empty use the len() method

Alternatively, you can use the len() method to check if the dictionary is empty or not. This function returns the length of the object in Python. Let’s check the length of the empty dictionary is ‘0‘ using the if condition. If it is ‘0’ it will print dictionary is empty, otherwise, it will print dictionary is not empty.


# Check if the dictionary is empty using len() method
empty_dict = {}
dict = len(empty_dict)
print("Given dictionary:", empty_dict)
if dict == 0:
    print ("Dictionary is empty")
else:
    print ("Dictionary is not empty")

# Output:
# Given dictionary: {}
# Dictionary is empty

8. Conclusion

In this article, I have explained how to create an empty dictionary using the curly braces({}), dict() constructer, and keys of Python dictionary with examples. And also explained using the bool() method, boolean operator(not), if statement, and len() method how we can check if the dictionary is empty or not.