Skip to main content

Python Dictionary

Introduction

In real-world programming scenarios, it is often not sufficient to merely store values independently, because many practical problems require us to maintain a relationship between two pieces of data, such as mapping a student’s roll number to their name, associating a mobile number with an address, or linking a product ID to its details, and in such cases, the concept of storing isolated values is no longer adequate.

To address this requirement, Python provides a powerful data structure known as the dictionary, which allows us to represent data in the form of key-value pairs, thereby enabling a direct and meaningful association between related pieces of information.

Understanding the Need for Dictionary

If you reflect on the previously discussed data types, you will realize that:

  • Lists, tuples, sets, and frozen sets all deal with individual values grouped together
  • None of them inherently express a relationship like “this value belongs to that key”

For example, if you want to store student data such as:

  • Roll number → Name
  • Mobile number → Address

you need a structure where one value acts as an identifier (key), and the other acts as the associated data (value), and this requirement naturally leads us to the dictionary data type.

What is a Dictionary?

A dictionary in Python is a collection of key-value pairs, where each key is associated with exactly one value, and together they form a mapping that allows efficient retrieval of data based on the key.

You can think of a dictionary in Python as being very similar to a real-world dictionary, where:

  • The word acts as the key
  • The meaning acts as the value

and this analogy helps in understanding how data is stored and accessed within a dictionary.

Syntax of Dictionary

A dictionary is represented using curly braces , where each entry consists of a key and a value separated by a colon.

d = {
100: "James",
200: "Roger",
300: "David"
}

Here:

  • 100, 200, 300 are keys
  • "James", "Roger", "David" are values

Each pair is separated by a comma, forming a complete dictionary.

Verifying Dictionary Type

print(type(d)) # <class 'dict'>

Creating Dictionary Dynamically

Sometimes, we may not know all key-value pairs at the beginning, and in such cases, we can create an empty dictionary and add entries later.

d = {}
d[100] = "James"
d[200] = "David"

print(d)

This approach is particularly useful when data is generated dynamically during program execution.

Important Behavior of Dictionary

Let us now explore the key properties of dictionaries in a more detailed and conceptual manner.

1. Keys and Values

Every element in a dictionary is a pair, where:

  • The key acts as an identifier
  • The value represents associated data

This pairing is what makes dictionaries fundamentally different from other collection types.

2. Duplicate Keys Are Not Allowed

One of the most important rules in dictionaries is that duplicate keys are not allowed, because each key must uniquely identify a value.

However, if you attempt to insert a duplicate key, instead of throwing an error, Python performs a replacement.

3. Duplicate Values Are Allowed

Unlike keys, values in a dictionary can be duplicated without any restriction.

d = {
100: "James",
200: "David",
300: "Roger"
}

Here, all values are the same. But keys are different. This is perfectly valid.

4. Order in Dictionary

In the context of this discussion, you should understand that dictionary entries are internally managed using hashing, and therefore the order in which elements appear is not something you should rely on conceptually, especially when learning the fundamentals.

This means that you should not write logic that depends on positional access within a dictionary.

5. Indexing and Slicing Are Not Applicable

Since dictionaries are not based on positional storage but rather on key-based access, operations like indexing and slicing are not applicable.

For example:

d[0] # Invalid
d[1:3] # Invalid

These operations do not make sense in the context of dictionaries.

6. Dictionary is Mutable

A dictionary is mutable, which means that after creation:

  • You can add new key-value pairs
  • You can update existing values
  • You can remove entries

This makes dictionaries highly flexible and suitable for dynamic data handling.

7. Heterogeneous Data Allowed

Dictionaries allow both keys and values to be of different types, meaning that you are not restricted to a single data type.

d = {
1: "Hello",
"id": 100,
3.5: True
}

This flexibility makes dictionaries extremely powerful in real-world applications.