Tutorials Home   >   Core Programming Concepts   >   Dictionaries

Dictionaries

1. Introduction to Dictionaries

In programming, data needs to be stored and accessed efficiently. While lists or arrays store data in an ordered sequence using index numbers, they are not always suitable for all types of data. In many real-life situations, data exists in the form of key–value pairs.

To handle such data, programming languages provide Dictionaries (in Python) or Maps (in languages like Java and C++). These data structures allow data to be stored in a meaningful and organized way using keys instead of numerical indexes.


2. What Are Dictionaries / Maps?

Definition:

A dictionary or map is a data structure that stores elements as key–value pairs, where each key is unique and is used to access its associated value.

Example (Real Life):

  • Dictionary: word → meaning

  • Student record: roll number → name

  • Phone contacts: name → phone number


3. Difference Between Lists and Dictionaries / Maps

Lists / Arrays Dictionaries / Maps
Accessed using index Accessed using keys
Ordered data Unordered or logically ordered
Duplicate values allowed Keys must be unique
Simple structure More flexible

4. Creating a Dictionary / Map (Python Example)

student = {
"name": "Amit",
"age": 16,
"class": 10
}

Here:

  • "name", "age", and "class" are keys

  • "Amit", 16, and 10 are values


5. Accessing Values in a Dictionary / Map

Values are accessed using their keys.

Example:

print(student["name"])

Output:

Amit

If a key does not exist, it may cause an error or return a default value.


6. Adding and Updating Elements

Adding a New Key–Value Pair:

student["marks"] = 85

Updating an Existing Value:

student["age"] = 17

Dictionaries are dynamic, meaning they can grow or change during program execution.


7. Removing Elements from a Dictionary / Map

Elements can be removed using different methods.

Example:

del student["class"]

Other methods include:

  • pop()

  • clear()


8. Traversing a Dictionary / Map

To access all elements in a dictionary, loops are used.

Example:

for key in student:
print(key, ":", student[key])

Using items():

for key, value in student.items():
print(key, value)

9. Dictionary Keys and Values

  • Keys must be unique

  • Keys are usually strings or numbers

  • Values can be of any data type

  • Keys cannot be changed, but values can


10. Common Uses of Dictionaries / Maps

Dictionaries/maps are widely used in:

  • Storing student records

  • Word count programs

  • Configuration settings

  • Database-like structures

  • Fast data lookup


11. Nested Dictionaries / Maps

A dictionary can contain another dictionary.

Example:

students = {
1: {"name": "Amit", "marks": 80},
2: {"name": "Riya", "marks": 90}
}

This structure is useful for storing complex data.


12. Advantages of Dictionaries / Maps

  • Fast data access

  • Flexible structure

  • Easy data organization

  • Efficient for large data sets


13. Disadvantages of Dictionaries / Maps

  • More memory usage

  • Unordered (in some languages)

  • Cannot use index positions

  • Complex structure for beginners


14. Common Mistakes by Learners

  1. Using duplicate keys

  2. Accessing non-existent keys

  3. Confusing keys with values

  4. Improper nesting

  5. Syntax errors


15. Best Practices for Using Dictionaries / Maps

  • Use meaningful keys

  • Keep structure simple

  • Check if key exists before accessing

  • Comment complex dictionaries

  • Avoid deeply nested structures

Example:

if "marks" in student:
print(student["marks"])

16. Dictionaries / Maps vs Multidimensional Lists

Dictionaries / Maps Multidimensional Lists
Accessed by keys Accessed by index
Flexible Fixed structure
Faster lookup Sequential access

17. Conclusion

Dictionaries or maps are powerful data structures that allow programmers to store data in the form of key–value pairs. They provide fast access and flexibility, making them ideal for real-world applications where data needs to be organized meaningfully.