Skip to content

Python Dictionary - A Comprehensive Guide

Overview

A dictionary in Python is a collection of key-value pairs that are unordered, mutable, and indexed. It is one of the most powerful and commonly used data structures in Python, allowing fast lookups, insertion, and deletion.

Key Characteristics

  • Key-Value Pairs: Each item in a dictionary is stored as a key-value pair.
  • Keys Are Unique: Keys must be unique and immutable (e.g., strings, numbers, tuples).
  • Mutable: Dictionaries can be modified after creation (e.g., adding, updating, or deleting items).
  • Dynamic: The size of a dictionary can grow or shrink dynamically.

Creating Dictionaries

Dictionaries can be created using curly braces {} or the dict() constructor.

# Using curly braces
empty_dict = {}
student = {"name": "Alice", "age": 22, "major": "Computer Science"}

# Using the dict() constructor
employee = dict(name="Bob", age=30, department="HR")

Accessing Dictionary Elements

By Key

You can access the value associated with a key using square brackets [] or the get() method.

student = {"name": "Alice", "age": 22, "major": "Computer Science"}

# Accessing elements
print(student["name"])  # Output: Alice
print(student.get("age"))  # Output: 22

Iterating Through a Dictionary

# Iterating through keys
for key in student:
    print(key, student[key])

# Iterating through key-value pairs
for key, value in student.items():
    print(f"{key}: {value}")

Modifying Dictionaries

Adding or Updating Elements

student = {"name": "Alice", "age": 22}
student["major"] = "Computer Science"  # Adding a new key-value pair
student["age"] = 23  # Updating an existing key-value pair
print(student)
# Output: {'name': 'Alice', 'age': 23, 'major': 'Computer Science'}

Removing Elements

  • pop(key): Removes the key-value pair for the specified key.
  • popitem(): Removes and returns the last inserted key-value pair.
  • del: Deletes a specific key or the entire dictionary.
  • clear(): Removes all items from the dictionary.
student = {"name": "Alice", "age": 22, "major": "Computer Science"}

# Removing an item
student.pop("age")
print(student)  # Output: {'name': 'Alice', 'major': 'Computer Science'}

# Clearing the dictionary
student.clear()
print(student)  # Output: {}

Dictionary Methods

Common Methods

  • keys(): Returns a view object of all keys.
  • values(): Returns a view object of all values.
  • items(): Returns a view object of all key-value pairs.
  • update(other_dict): Updates the dictionary with another dictionary.
student = {"name": "Alice", "age": 22}
print(student.keys())   # Output: dict_keys(['name', 'age'])
print(student.values()) # Output: dict_values(['Alice', 22])
print(student.items())  # Output: dict_items([('name', 'Alice'), ('age', 22)])

# Updating the dictionary
student.update({"major": "Computer Science"})
print(student)
# Output: {'name': 'Alice', 'age': 22, 'major': 'Computer Science'}

Difference: Tuple vs List vs Set vs Dictionary

Feature Tuple List Set Dictionary
Ordered Yes Yes No No
Mutable No Yes Yes Yes (keys immutable)
Duplicates Allowed Yes Yes No Keys: No, Values: Yes
Access by Index Yes Yes No No
Syntax (1, 2) [1, 2] {1, 2} {"key": "value"}

Difference: Dictionary vs JSON

Feature Dictionary JSON
Format Python-specific Language-neutral
Syntax {} for key-value pairs Text-based (string)
Keys Must be immutable and hashable Must be strings
Boolean Values True/False true/false
Null Representation None null
Data Types Python objects Strings, numbers, arrays
Use Case In-memory operations Data exchange

Syntax and Code Examples

Dictionary Example

data = {"name": "Alice", "age": 22, "is_student": True}
print(data["name"])  # Output: Alice

JSON Example

import json

# Convert dictionary to JSON
data = {"name": "Alice", "age": 22, "is_student": True}
data_json = json.dumps(data)
print(data_json)  # Output: '{"name": "Alice", "age": 22, "is_student": true}'

# Convert JSON to dictionary
data_dict = json.loads(data_json)
print(data_dict["name"])  # Output: Alice