Skip to content

Python create empty dictionary

  • by

In Python, you can create an empty dictionary by using curly braces {} or by using the built-in dict() function without any arguments. Here’s how you can do it:

Using curly braces:

empty_dict = {}

Using the dict() function:

empty_dict = dict()

Both of these approaches will create an empty dictionary named empty_dict. You can then add key-value pairs to this dictionary as needed. For example:

empty_dict['key1'] = 'value1'
empty_dict['key2'] = 42

Or, you can initialize a dictionary with some initial values:

initial_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

Note: dictionaries in Python are mutable, and you can add, modify, or remove key-value pairs freely.

Python creates an empty dictionary example

Here’s an example of creating an empty dictionary and then adding some key-value pairs to it:

# Create an empty dictionary
empty_dict = {}
print(empty_dict)

# Adding key-value pairs to the dictionary
empty_dict['name'] = 'Alice'
empty_dict['age'] = 25
empty_dict['city'] = 'London'

# Printing the dictionary
print(empty_dict)

Output:

Python create empty dictionary

In this example, we first create an empty dictionary named empty_dict using curly braces {}. Then, we add three key-value pairs to the dictionary: ‘name’: ‘Alice’, ‘age’: 25, and ‘city’: ‘London’. Finally, we print the dictionary to see the result. As you can see, the dictionary contains the added key-value pairs.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading