Dictionaries in Python

1. What is a Dictionary?

A dictionary is a data structure that stores data as key-value pairs.

  • Key → identifier
  • Value → associated data

Example concept:

  • "pen 2" → "zebras"

Key Point:
Dictionary = key → value mapping


2. Why Dictionaries are Important

Dictionaries are widely used because they:

  • Provide fast data lookup
  • Store structured data efficiently
  • Make it easy to retrieve specific information

Key Point:
Dictionaries are essential for data analysis


3. Key-Value Concept

Think of a real dictionary:

  • Word → definition

In Python:

  • Key → value

Access:

  • Use key to get value

Key Point:
You search by key, not by value


4. Creating a Dictionary

Method 1: Using braces {}

zoo = {1: "lion", 2: "zebra", 3: "elephant"}
  • Key and value separated by :
  • Pairs separated by ,

Method 2: Using dict() function

zoo = dict(pen1="lion", pen2="zebra")
  • Uses = instead of :
  • Keys become strings automatically

Key Point:
Two main ways to create dictionaries


5. Accessing Values

Use key inside brackets:

zoo[2]

Returns:

  • Value associated with key

Key Point:
Access by key, not position


6. KeyError

If key does not exist:

  • Python throws KeyError

Example:

  • Searching for value as key

Key Point:
Only keys can be used for lookup


7. Adding New Elements

You can add key-value pairs:

zoo[4] = "crocodile"

Key Point:
Dictionaries are mutable


8. Key Requirements

Keys must be immutable:

Allowed:

  • Strings
  • Integers
  • Floats
  • Tuples

Not allowed:

  • Lists
  • Sets
  • Dictionaries

Key Point:
Keys must not change


9. Dictionaries are Unordered

  • No fixed order
  • Cannot use index like list

Example:

zoo[0]  # KeyError

Key Point:
Dictionaries are not position-based


10. Order Behavior

  • Order may change internally
  • Do not rely on position

If order matters:

  • Use a list instead

Key Point:
Use appropriate data structure


11. Checking Keys

Use in keyword:

2 in zoo

Returns:

  • True / False

Important:

  • Works only for keys

Key Point:
in checks key existence


12. Alternative Creation Methods

You can create dictionaries from:

  • List of lists
  • List of tuples
  • Tuple of tuples

Example concept:

dict([(1, "lion"), (2, "zebra")])

Key Point:
Flexible creation options


13. Practical Use Cases

Dictionaries are used for:

  • User data (ID → info)
  • Configuration settings
  • Lookup tables
  • Data transformation

Key Point:
Perfect for mapping relationships


14. Lists vs Dictionaries

FeatureListDictionary
AccessIndexKey
OrderOrderedUnordered
Use caseSequenceMapping

Key Point:
List = sequence, Dictionary = mapping


15. Importance for Data Professionals

Dictionaries enable:

  • Fast data retrieval
  • Efficient data organization
  • Scalable data processing

Key Point:
Core structure for real-world data systems


Final Summary

A dictionary is a powerful Python data structure that stores data in key-value pairs. It allows fast and efficient data retrieval using keys, making it ideal for mapping relationships and organizing complex datasets. Dictionaries are mutable, but their keys must be immutable. Unlike lists, dictionaries are unordered and accessed by keys rather than indices. They are one of the most essential tools for data professionals working with structured data.


Key Takeaways

  • Dictionary = key-value pairs
  • Access values using keys
  • Keys must be immutable
  • Dictionaries are unordered
  • Use {} or dict() to create
  • in checks keys only
  • Faster than lists for lookup
  • Essential for data mapping

Similar Posts

Questions, corrections, or additional insights?