Python create empty Dictionary [3 methods]

In this article, I will explain How can Python create empty Dictionary with different methods present and we will also see some other dictionaries that can be called empty dictionary Python will illustrative examples.

Additionally, we will also see some methods present in Python that will help us to check if the created things is an empty dictionary in Python or not.

Dictionary in Python

A dictionary in Python is a built-in data type that can hold a mutable, unordered collection of items as key-value pairs. As a dictionary can contain multiple data types, it is used for various purposes in Python. Each key in a dictionary must be unique and each key is associated with a value, making it a key-value pair.

Here’s how you can create a dictionary in Python: In Python, a dictionary is defined by enclosing key-value pairs in curly braces ({}), with the key and value separated by a colon (:).

my_dict = {'name': 'Alice', 'age': 25}
print(type(my_dict))
print(my_dict)

Output: We are checking the type of the variable to verify whether the variable store is a Python dictionary or not.

<class 'dict'>
{'name': 'Alice', 'age': 25}
python create empty dictionary

Python define empty Dictionary

In Python, an empty dictionary is a dictionary that contains no items (i.e., no key-value pairs). You can define an empty dictionary by using various methods present in Python, such as:

  • Using Curly Braces {}
  • Using dict() Constructor
  • Using dictionary comprehension

Let’s see them one by one using demonstrative examples.

Note: We check whether what we have created is an empty dictionary or not, we have three different functions in Pythion:

Name Description
type() function The type() function returns the type of the specified object in Python.
bool() function The bool() function converts a value to Boolean (True or False). It is used to evaluate any value or expression. If the value or expression equates to True, then bool() returns True; otherwise, it returns False.
len() function The len() function will return the number of items present in the Python dictionary.
A list of Python functions to check the dictionary is empty.

Method 1: Create empty Dictionary Python with curly braces

The most straightforward way to create an empty dictionary in Python is to use empty curly braces {}.

Example 1: we will create an empty dictionary Python named states_and_capitals which is intended to store US states as keys and their capitals as values. This dictionary will be filled as data becomes available.

# Creating an empty dictionary to store the states and their capitals
states_and_capitals = {}

Example 2: For instance, Imagine you are developing a program to store information about the 50 states in the USA. Initially, you don’t have any data, so you start by creating an empty Python dictionary. Later, you can add states as keys and their information as values.

states_info = {}
print(states_info)
print(type(states_info))
print(bool(states_info))

Output: To check the thing we have created and stored in the Python variable, Firstly we check the data type and then find whether it is considered as fasly in Python.

{}
<class 'dict'>
False
how to create empty dictionary in python

This confirms the variable contains a dictionary with no value i.e., we have successfully Python create empty Dictionary.

Let’s add some key-value pairs to it and find out the number of items present inside it after adding.

states_info = {}
states_info['California'] = {'Capital': 'Sacramento', 'Population': 39538223}
states_info['Texas'] = {'Capital': 'Austin', 'Population': 29145505}
print(states_info)
print(len(states_info))

Output:

{'California': {'Capital': 'Sacramento', 'Population': 39538223}, 'Texas': {'Capital': 'Austin', 'Population': 29145505}}
2
define empty dictionary python

This way we can check if Python create empty Dictionary.

Method 2: Python create an empty Dictionary using dict() method

Another method to create a Python empty dictionary is by using the dict() constructor.

Example 1: In this example, an empty Python dictionary city_population is created to store the populations of different cities in the USA. This dictionary can be populated later as data is gathered.

# Creating an empty dictionary to store the population of different US cities
city_population = dict()

Example 2: Suppose you’re working on a project to catalog all the national parks in the USA. You can start by creating an empty dictionary in Python and then populate it with national park names as keys and relevant information (such as location, size, and established year) as values.

national_parks = dict()
print(national_parks)
print(type(national_parks))
print(bool(national_parks))
print(len(national_parks))

Output:

{}
<class 'dict'>
False
0
empty dictionary in python

This way we can check if Python create empty dictionary is actually empty or not.

Let’s add some key-value pairs to it and find out the number of items present inside it after adding.

national_parks = dict()
national_parks['Yellowstone'] = 'Wyoming'
national_parks['Yosemite'] = 'California'
print(national_parks)
print(bool(national_parks))
print(len(national_parks))

Output:

{'Yellowstone': 'Wyoming', 'Yosemite': 'California'}
True
2
python initialize empty dictionary

This way we check if Python create empty Dictionary

Method 3: Python declare empty dictionary using dictionary comprehension

In Python, dictionary comprehension provides a concise way to create dictionaries. Although it’s more commonly used to create populated dictionaries, it can also be used to create an empty dictionary in Python.

Example 1:

# Using dictionary comprehension to create an empty dictionary
empty_dict = {k: v for k, v in []}

Example 2: Suppose you’re working on a project to catalog some of the popular companies in the USA. You can start by creating an empty dictionary in Python and then populate it with the company names as keys and relevant information (which category products they manufacture) as values.

companies = {k: None for k in []}
print(companies)
print(type(companies))
print(bool(companies))
print(len(companies))

Output:

{}
<class 'dict'>
False
0
how to create an empty dictionary in python

Let’s add some key-value pairs to it and find out the number of items present inside it after adding.

companies = {k: None for k in []}
companies['Apple'] = 'Gadgets'
companies['Oreo'] = 'Food'
print(companies)
print(bool(companies))
print(len(companies))

Output:

{'Apple': 'Gadgets', 'Oreo': 'Food'}
True
2
initialize empty dictionary python

This way we in Python create empty Dictionary using dictionary comprehension.

These were the methods by which we could create an empty dictionary in Python. But, there are many other dictionaries in Python that can be called empty dictionaries.

Other empty Python dictionary

There are many other dictionaries in Python that can be considered as empty and we can create them in Python. Let’s see them one by one:

Python create empty dictionary with keys

When you need a dictionary with predefined keys but no values, the dict.fromkeys() method is helpful. This method returns a new dictionary with the given keys and a specified value (default is None).

In the example of a store chain across the USA:

store_locations = ['New York', 'California', 'Texas']
store_sales = dict.fromkeys(store_locations, None)
print(store_sales)

Output:

{'New York': None, 'California': None, 'Texas': None}
create empty dictionary python with keys

This way, Python create empty dictionary with keys using dict.fromkeys() method.

Create an Empty Nested Dictionary in Python

When dealing with hierarchical data, an empty nested dictionary may be used. It’s a dictionary where some keys have dictionaries as values.

In the vaccine distribution example:

vaccine_distribution = {'California': {}, 'Texas': {}}
print(vaccine_distributed)

Output: The vaccine_distribution is a Python dictionary with states as keys (‘California’ and ‘Texas’) and their values as empty dictionaries ({}).

{'California': {}, 'Texas': {}}
empty dictionary python

This way we can Python create empty Dictionary which is nested.

Conclusion

Understanding how Python create empty Dictionary using three different ways using curly braces {}, the dict() constructor, and the dictionary comprehension with illustrative examples after creating we need to check whether the dictionary is empty or not with the type(), bool(), and len() functions in Python.

Lastly, I have also explained what other kinds of dictionaries can be considered empty in Python.

You may also like reading the following tutorials.


电子产品 排行榜


Gurmandise 哆啦A梦 铜锣烧 手机手环

IT電腦補習 java補習 為大家配對電腦補習,IT freelance, 私人老師, PHP補習,CSS補習,XML,Java補習,MySQL補習,graphic design補習,中小學ICT補習,一對一私人補習和Freelance自由工作配對。
立刻註冊及報名電腦補習課程吧!

facebook 查詢:
24 hours enquiry facebook channel :
https://www.facebook.com/itteacheritfreelance/?ref=aymt_homepage_panel

Be the first to comment

Leave a Reply

Your email address will not be published.


*