To create an empty dictionary in Python, you can use curly braces `{}` or the `dict()` constructor. A dictionary is a collection of key-value pairs where each key must be unique.
We can create an empty dictionary in Python using curly braces `{}`. This example demonstrates how to initialize an empty dictionary and add key-value pairs to it.
For example,
my_dict
using curly braces.my_dict = {}
# Add key-value pairs to the dictionary
my_dict[1] = 'One'
my_dict[2] = 'Two'
my_dict[3] = 'Three'
# Print the dictionary contents
print('Dictionary contents:')
for key, value in my_dict.items():
print(f'Key: {key}, Value: {value}')
Dictionary contents: Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three
We can create an empty dictionary in Python using the `dict()` constructor. This example demonstrates how to initialize an empty dictionary and add key-value pairs to it.
For example,
my_dict
using the `dict()` constructor.my_dict = dict()
# Add key-value pairs to the dictionary
my_dict['A'] = 1.1
my_dict['B'] = 2.2
my_dict['C'] = 3.3
# Print the dictionary contents
print('Dictionary contents:')
for key, value in my_dict.items():
print(f'Key: {key}, Value: {value}')
Dictionary contents: Key: A, Value: 1.1 Key: B, Value: 2.2 Key: C, Value: 3.3
In this tutorial, we learned How to create an Empty Dictionary in Python language with well detailed examples.