Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create dictionary in Python

Understanding the Concept of Dictionaries

In the world of Python programming, a dictionary is like a real-life dictionary. You have a word, or what we call a "key", and that word has a definition, or a "value". So, in Python, a dictionary (also known as a 'dict') is a collection of key-value pairs. Each key-value pair maps the key to its associated value.

The Basics of Creating a Dictionary

Creating a dictionary in Python is quite simple and straightforward. You start by defining your dictionary using curly brackets {}. Inside these brackets, you include your key-value pairs, separated by commas.

For instance, let's say we want to create a dictionary of fruits and their colors. Here's how we can do that:

fruit_color = {"apple": "red", "banana": "yellow", "grape": "purple"}

In this dictionary, "apple", "banana", and "grape" are keys and "red", "yellow", and "purple" are their respective values.

Accessing Values in a Dictionary

To access the value for a particular key, you use the key inside square brackets []. Let's use our fruit_color dictionary to get the color of an apple:

print(fruit_color["apple"])

This will output: "red"

Modifying Values in a Dictionary

Let's say we discovered a new type of apple that's green. We can change the value of the key "apple" in our dictionary like this:

fruit_color["apple"] = "green"

Now, if we print the color of an apple, it will output: "green"

Adding New Key-Value Pairs

To add a new key-value pair, you simply use a new key and assign a value to it. Let's add a "pear" to our dictionary:

fruit_color["pear"] = "green"

We now have four fruits in our dictionary.

Removing Key-Value Pairs

To remove a key-value pair, you use the del statement. Let's say we want to remove "grape" from our dictionary:

del fruit_color["grape"]

The grape and its color are now removed from our dictionary.

Looping Through a Dictionary

Python makes it easy to loop through a dictionary. You can loop through all key-value pairs, all keys, or all values. Here's how you can do each:

Looping Through All Key-Value Pairs

for fruit, color in fruit_color.items():
    print(f"The color of {fruit} is {color}.")

Looping Through All Keys

for fruit in fruit_color.keys():
    print(f"The fruit is {fruit}.")

Looping Through All Values

for color in fruit_color.values():
    print(f"The color is {color}.")

Nesting Dictionaries

Sometimes, you might want to store multiple dictionaries in a list, or a list of items as a value in a dictionary. This is called nesting. You can nest dictionaries inside a dictionary, inside a list, and vice versa.

For example, let's say we want to store information about two different people in a dictionary. We can nest two dictionaries inside a list like this:

person_1 = {'name': 'John', 'age': 25}
person_2 = {'name': 'Sarah', 'age': 28}

people = [person_1, person_2]

for person in people:
    print(person)

This will output:

{'name': 'John', 'age': 25}
{'name': 'Sarah', 'age': 28}

Conclusion: The Power of Python Dictionaries

Just like a real-life dictionary, Python dictionaries offer a simple and intuitive way to organize, store, and access data. Whether you're tracking the color of fruits, storing information about people, or managing game data, dictionaries provide a flexible, efficient, and "Pythonic" way to structure your data.

Remember, the key (pun intended!) to mastering dictionaries, like any other Python concept, is practice. So, try creating different dictionaries, adding and removing key-value pairs, and looping through your dictionaries. Before you know it, you'll be a dictionary whiz, capable of harnessing the power of this versatile data structure to write clean, efficient Python code. Happy coding!