Blog Post #64: Beyond the Index: An Introduction to Dictionaries

So far in Part 3, we’ve explored lists and tuples. Both are powerful sequence types that store collections of items in a specific order, which we access using integer indexes like 0, 1, and 2 (a concept we learned in Post #47).

But what if we don’t want to access data by its numerical position? What if we want to look up data using a meaningful label, like a word in a dictionary? For this, Python provides its most flexible built-in data structure: the dictionary.

In this post, we’ll introduce the dictionary, Python’s primary mapping type, and understand its powerful concept of storing data in key-value pairs.

The Problem with Integer Indexes

Imagine you want to store information about a user. With a list, you might do this:

user_data = ["Alice", 30, "alice@email.com"]

To get the user’s email, you’d have to use user_data[2]. This works, but it’s not very readable or reliable. You have to remember what index corresponds to what piece of information. If you were to add a new piece of data in the middle of the list, all your indexes would shift, and your code would break.

The Dictionary: A Better Way to Label Data

A dictionary solves this problem by letting you store data in key-value pairs. Instead of a numerical index, you use a unique, descriptive key to store and retrieve a value.

It works just like a real-world dictionary: you look up a word (the key) to find its definition (the value).

Let’s break down the two components:

  • Key: A unique identifier for a value. Keys must be of an immutable type. By far the most common type for a key is a string. Numbers and tuples can also be keys.
  • Value: The data associated with a key. Values can be of any data type: a number, a string, a boolean, a list, or even another dictionary.

Creating a Dictionary

You create a dictionary using curly braces {}, with each key-value pair written as key: value and separated by commas.

Let’s rewrite our user data example as a dictionary. It’s conventional to write larger dictionaries on multiple lines for readability.

# A dictionary representing a user profile
user_profile = {
    "name": "Alice",
    "age": 30,
    "email": "alice@email.com",
    "is_active": True
}

print(user_profile)

This is much more readable and robust. We no longer have to remember that index 2 is the email; we can simply look for the “email” key.

You can also create an empty dictionary, which you can add to later.

# An empty dictionary
empty_dictionary = {}

What’s Next?

You’ve now been introduced to the dictionary, a powerful data structure for storing labeled data in key-value pairs. Dictionaries are incredibly flexible and are one of the most frequently used data types in Python.

Now that we know how to create a dictionary, how do we use it? How do we look up a value using its key, add new key-value pairs, or change the value of an existing key? In Post #65, we will cover the fundamental operations for working with dictionaries.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment