Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to initialize a dictionary in Python

Getting Started with Dictionaries in Python

In the world of Python, a dictionary is a built-in data type that holds a collection of key-value pairs. But if you're new to programming, you might be asking, what is a key-value pair?

Imagine a real-life dictionary. It consists of words (the keys), and the corresponding definitions (the values). Similarly, in Python, a dictionary contains keys and values but these can be of any data type – numbers, strings, lists, or even another dictionary.

Let's start with a simple example of initializing a dictionary in Python:

my_dictionary = {}

Here, we've created an empty dictionary named my_dictionary. The {} is a shorthand way of saying, "This is a dictionary."

Adding Items to Your Dictionary

Once we have our empty dictionary, we can start adding items. Each item in a dictionary is a key-value pair. The key is the 'word' and the value is the 'definition'. Here's how to add an item to the dictionary:

my_dictionary = {}  # Start with an empty dictionary
my_dictionary['apple'] = 'A sweet, edible fruit'  # Add a key-value pair

In this example, the key is 'apple' and the value is 'A sweet, edible fruit'. You can imagine the key as the name of a box, and the value as the item inside the box. You can open the box using the name to get the item inside.

Initializing a Dictionary with Multiple Items

You can initialize a dictionary with multiple items at once. Here's how:

my_dictionary = {
    'apple': 'A sweet, edible fruit',
    'banana': 'A long, curved fruit',
    'cherry': 'A small, round fruit',
}

In this example, we've initialized a dictionary with three key-value pairs. Each pair is separated by a comma, and the key and value within each pair are separated by a colon.

Accessing Dictionary Values

Now that we have a dictionary with some items, let's learn how to access these items. We can do this using the keys:

my_dictionary = {
    'apple': 'A sweet, edible fruit',
    'banana': 'A long, curved fruit',
    'cherry': 'A small, round fruit',
}

print(my_dictionary['apple'])  # Prints: A sweet, edible fruit

In this example, we've accessed the value of 'apple' using the key and printed it. It's like saying, "Give me the definition of 'apple'."

Checking if a Key Exists

What if we try to access a key that doesn't exist in the dictionary? Python will raise a KeyError. To avoid this, we can check if a key exists in the dictionary before accessing its value:

my_dictionary = {
    'apple': 'A sweet, edible fruit',
    'banana': 'A long, curved fruit',
    'cherry': 'A small, round fruit',
}

if 'apple' in my_dictionary:
    print(my_dictionary['apple'])  # Prints: A sweet, edible fruit
else:
    print('The key does not exist.')

This is similar to checking if a word exists in a dictionary before looking up its definition.

Conclusion

Python dictionaries are like real-life dictionaries. They hold key-value pairs, where the keys are like the words in a dictionary, and the values are like the definitions. Dictionaries are initialized using {} and items can be added or accessed using the keys.

Just like how a dictionary can help you understand a new language, Python dictionaries can help you manage and use data efficiently in your code. They are a powerful tool in the Python programming language, and getting comfortable with them will open up new possibilities in your coding journey.

So next time you come across a pile of data, just remember – there's probably a Python dictionary that can help you sort it out. Happy coding!