Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is dictionary in Python

Understanding the Basics of Dictionaries in Python

Imagine you have a real-life dictionary in your hands, filled with words and their meanings. In the world of Python programming, a dictionary (often abbreviated as dict) plays a similar role, but instead of words and definitions, it holds pairs of keys and values. A key can be thought of as the word you're looking up, and the value is the meaning associated with that word.

What Exactly Is a Python Dictionary?

A Python dictionary is a built-in data type that allows you to store data in pairs known as key-value pairs. Each key is connected to a specific value, and you can use the key to access the value associated with it. The keys in a dictionary are unique, which means no two keys can be the same. This uniqueness ensures that each key points to one specific value.

Creating Your First Dictionary

Let's jump right into how you can create a dictionary in Python. You can define a dictionary by using curly braces {} with pairs of keys and values separated by colons. Here's a simple example:

# Creating a dictionary with three key-value pairs
my_dictionary = {
    'name': 'Alice',
    'age': 25,
    'city': 'New York'

In this example, 'name', 'age', and 'city' are keys, and 'Alice', 25, and 'New York' are their corresponding values.

Accessing Dictionary Values

Once you have a dictionary, you might want to retrieve the value associated with a specific key. You can do this by using square brackets [] and the key name:

# Accessing the value associated with the key 'name'
print(my_dictionary['name'])  # Output: Alice

What If the Key Doesn't Exist?

Trying to access a value for a key that doesn't exist in the dictionary will result in an error. To handle this, you can use the get method, which allows you to specify a default value if the key is not found:

# Using get to access a value with a default
print(my_dictionary.get('occupation', 'Not specified'))  # Output: Not specified

Adding and Modifying Dictionary Entries

Adding a new key-value pair to an existing dictionary is straightforward. You assign a value to a new key, and if the key already exists, its value will be updated:

# Adding a new key-value pair
my_dictionary['occupation'] = 'Engineer'

# Modifying the value of an existing key
my_dictionary['age'] = 26

Removing Entries from a Dictionary

To remove an entry from a dictionary, you can use the del statement or the pop method:

# Removing an entry using del
del my_dictionary['city']

# Removing an entry using pop and storing the removed value
removed_age = my_dictionary.pop('age')

Iterating Over a Dictionary

You might want to go through all the entries in a dictionary to perform certain operations. You can iterate over the keys, values, or both:

# Iterating over keys
for key in my_dictionary:

# Iterating over values
for value in my_dictionary.values():

# Iterating over key-value pairs
for key, value in my_dictionary.items():
    print(key, value)

Dictionary Comprehensions

Just like list comprehensions provide a concise way to create lists, dictionary comprehensions offer a way to create dictionaries based on existing iterables:

# Creating a dictionary with dictionary comprehension
squares = {number: number**2 for number in range(5)}
print(squares)  # Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Nested Dictionaries

A dictionary can contain another dictionary, allowing you to create a nested data structure:

# Creating a nested dictionary
nested_dict = {
    'person1': {'name': 'Alice', 'age': 25},
    'person2': {'name': 'Bob', 'age': 30}

# Accessing data in a nested dictionary
print(nested_dict['person1']['name'])  # Output: Alice

Intuition and Analogies to Help You Understand

Think of a dictionary as a customizable filing cabinet where each drawer (key) contains a folder (value). You can label the drawers with unique names, ensuring you can quickly find the exact folder you need. You can add new drawers, relabel them, replace the contents of a folder, or even remove a drawer entirely.

Conclusion: Embracing the Power of Dictionaries

Dictionaries in Python are an incredibly powerful and flexible data structure. They allow you to create a collection of related data with fast access to each item using keys. As you become more familiar with dictionaries, you'll find them indispensable for organizing and managing data in your Python programs.

From storing user information to managing complex configurations, dictionaries can handle a wide range of tasks with ease. They are a testament to Python's commitment to writing clear, concise, and readable code. As you continue your programming journey, you'll discover that dictionaries, much like their real-life counterparts, are a treasure trove of information, just waiting to be explored.