← Back to Articles
Tutorial

Creating a Dictionary in Python

Learn how to create and manage dictionaries in Python effectively.

Python dictionaries are a versatile data structure that store key-value pairs. They are used for various applications, from simple data storage to complex data manipulation.

To create a dictionary in Python, you can use curly braces {} or the dict() function. For example, my_dict = {'key1': 'value1', 'key2': 'value2'} creates a simple dictionary.

When creating dictionaries, use descriptive keys, ensure keys are immutable, and remember that dictionaries are unordered, which means they do not maintain any order.

Avoid using mutable data types as keys, and remember that dictionary keys must be unique. If a key is repeated, the last assigned value will overwrite the previous one.

Code Examples

Example 1

my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}

Example 2

my_dict = dict(name='Bob', age=30, city='Los Angeles')

More Python Tutorials