gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
How do you create a dictionary in Python?
- User
How do you create a dictionary in Python?
- Assistant
In Python, you can create a dictionary using curly braces
{}or thedict()constructor. A dictionary is a collection of key-value pairs, where each key is unique.Method 1: Using Curly Braces
You can create a dictionary by enclosing key-value pairs in curly braces, using a colon to separate keys and values.
# Creating a dictionary using curly braces my_dict = { 'name': 'Alice', 'age': 30, 'city': 'New York' } print(my_dict)Method 2: Using the
dict()ConstructorYou can also create a dictionary by using the
dict()constructor, where you define key-value pairs using keyword arguments.# Creating a dictionary using the dict() constructor my_dict = dict(name='Alice', age=30, city='New York') print(my_dict)