How to create a dictionary in Python

In this Python tutorial, we will discuss, what is a dictionary in Python and how to create a dictionary in Python.

Dictionary in Python

In Python, a dictionary is a versatile data structure that can store multiple values that are associated with unique keys. This association between keys and values is often referred to as a key-value pair. It’s like an actual dictionary where you have words (keys) and their definitions (values).

A Python dictionary is an unordered collection, meaning that the items are not held in the order they were added, and they cannot be accessed by a numeric index. Instead, values are accessed through their keys, which makes it a highly efficient structure for certain types of data access.

One of the most important characteristics of Python dictionaries is that they are mutable, meaning that after a dictionary has been created, you can change, add, and remove items from it. Furthermore, keys must be immutable and unique within a dictionary, while values can be of any type and can be repeated.

Create a dictionary in Python

We can create a dictionary in Python by enclosing a comma-separated list of key-value pairs in curly braces {}. A colon : separates each key from its associated value.

Here’s a step-by-step guide on how to create a dictionary in Python:

  • Initialize a Dictionary: Start by defining your dictionary. You can name it anything you like. In this example, let’s call our dictionary usa_cities.
usa_cities = {}
  • At this point, usa_cities is an empty dictionary.
  • Populate the Dictionary: Add key-value pairs to the dictionary. The key and value are separated by a colon :, and each pair is separated by a comma. For instance, we can add cities as keys and their corresponding states as values.
usa_cities = {
    'New York': 'New York',
    'Los Angeles': 'California',
    'Chicago': 'Illinois',
    'Houston': 'Texas',
    'Phoenix': 'Arizona'
}

In this case, ‘New York’, ‘Los Angeles’, ‘Chicago’, ‘Houston’, and ‘Phoenix’ are the keys, and ‘New York’, ‘California’, ‘Illinois’, ‘Texas’, and ‘Arizona’ are their corresponding values.

Remember that the keys in a dictionary must be unique and immutable. They can be of any type such as strings, integers, tuples, etc. The values, however, can be of any type and can repeat in the dictionary.

  • Access the Dictionary: Now that the dictionary is set up, you can access any value by indicating its key in square brackets.
print(usa_cities['Chicago'])  # Outputs: Illinois

Here you can see the output:

create a dictionary in Python
create a dictionary in Python

This is how to create a dictionary in Python.

Modify a Python Dictionary

You can modify a Python dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an entry.

To add an element to Python dictionary:

usa_cities['Boston'] = 'Massachusetts'
print(usa_cities)

# Outputs: {'New York': 'New York', 'Los Angeles': 'California', 'Chicago': 'Illinois', 'Houston': 'Texas', 'Phoenix': 'Arizona', 'Boston': 'Massachusetts'}

To modify an existing item from Python Dictionary:

usa_cities['Boston'] = 'New England'
print(usa_cities)

# Outputs: {'New York': 'New York', 'Los Angeles': 'California', 'Chicago': 'Illinois', 'Houston': 'Texas', 'Phoenix': 'Arizona', 'Boston': 'New England'}

To delete an item from Python Dictionary:

del usa_cities['Boston']
print(usa_cities)

# Outputs: {'New York': 'New York', 'Los Angeles': 'California', 'Chicago': 'Illinois', 'Houston': 'Texas', 'Phoenix': 'Arizona'}

You may also like the following tutorials:

Here, in this Python tutorial, we learned, what is a dictionary in Python, and how to make a dictionary in Python with examples.