Create a Dictionary in Python

  • Post category:Dictionary

In Python, dictionaries are versatile data structures that allow you to store and manipulate data in key-value pairs. In this blog post, we will explore how to create a dictionary in Python using different methods.

Method 1: Using Curly Braces

# Method 1: Using Curly Braces
my_dict = {"name": "John", "age": 30, "city": "New York"}
print(my_dict)

Output:

{‘name’: ‘John’, ‘age’: 30, ‘city’: ‘New York’}

Method 2: Using the dict() Constructor

# Method 2: Using the dict() Constructor
my_dict = dict(name="John", age=30, city="New York")
print(my_dict)

Output:

{‘name’: ‘John’, ‘age’: 30, ‘city’: ‘New York’}