Skip to content

Latest commit

 

History

History
374 lines (287 loc) · 12.4 KB

File metadata and controls

374 lines (287 loc) · 12.4 KB

Exploring Python Dictionary Methods

Slide 1: Dictionary Clear Method

The clear() method efficiently removes all items from a dictionary, resulting in an empty dictionary. This operation is performed in-place, meaning it modifies the original dictionary rather than creating a new one, which is memory efficient for large dictionaries.

# Initialize a sample dictionary
sample_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(f"Original dictionary: {sample_dict}")

# Clear all items from the dictionary
sample_dict.clear()
print(f"After clear(): {sample_dict}")  

# Output:
# Original dictionary: {'name': 'John', 'age': 30, 'city': 'New York'}
# After clear(): {}

Slide 2: Dictionary Copy Method

The copy() method creates a new shallow copy of a dictionary. This means it creates a new dictionary containing references to the original dictionary's objects. Important for creating independent copies while preserving memory efficiency.

# Original dictionary with nested structure
original = {'name': 'John', 'info': {'age': 30, 'city': 'New York'}}

# Create a shallow copy
copied = original.copy()

# Modify the copied dictionary
copied['name'] = 'Jane'
copied['info']['age'] = 25

print(f"Original: {original}")
print(f"Copied: {copied}")

# Output:
# Original: {'name': 'John', 'info': {'age': 25, 'city': 'New York'}}
# Copied: {'name': 'Jane', 'info': {'age': 25, 'city': 'New York'}}

Slide 3: Dictionary FromKeys Method

fromkeys() is a versatile class method that creates a new dictionary from specified keys with optional default values. This method is particularly useful when initializing dictionaries with uniform values or creating template dictionaries.

# Creating a dictionary with default values
keys = ['apple', 'banana', 'orange']
default_value = 0

# Create dictionary using fromkeys
inventory = dict.fromkeys(keys, default_value)
print(f"Initial inventory: {inventory}")

# Using fromkeys with None as default value
template = dict.fromkeys(['name', 'age', 'email'])
print(f"Template dictionary: {template}")

# Output:
# Initial inventory: {'apple': 0, 'banana': 0, 'orange': 0}
# Template dictionary: {'name': None, 'age': None, 'email': None}

Slide 4: Dictionary Get Method

The get() method safely retrieves values from a dictionary by providing a default return value if the key doesn't exist. This prevents KeyError exceptions and simplifies error handling in dictionary operations.

# Sample dictionary
user_data = {'name': 'Alice', 'age': 25}

# Safe value retrieval with get()
name = user_data.get('name', 'Unknown')
email = user_data.get('email', 'No email provided')
score = user_data.get('score', 0)

print(f"Name: {name}")
print(f"Email: {email}")
print(f"Score: {score}")

# Output:
# Name: Alice
# Email: No email provided
# Score: 0

Slide 5: Dictionary Items Method

The items() method returns a view object containing tuple pairs of dictionary key-value pairs. This view dynamically reflects changes to the dictionary and is essential for dictionary iteration and comprehension.

# Initialize dictionary
student = {'name': 'Bob', 'grade': 'A', 'subjects': ['Math', 'Physics']}

# Iterate through items
for key, value in student.items():
    print(f"Key: {key}, Value: {value}")

# Using items in dictionary comprehension
uppercase_dict = {k.upper(): v for k, v in student.items()}
print(f"Uppercase keys: {uppercase_dict}")

# Output:
# Key: name, Value: Bob
# Key: grade, Value: A
# Key: subjects, Value: ['Math', 'Physics']
# Uppercase keys: {'NAME': 'Bob', 'GRADE': 'A', 'SUBJECTS': ['Math', 'Physics']}

Slide 6: Dictionary Keys Method

The keys() method returns a dynamic view object of dictionary keys. This view automatically updates when the dictionary changes and provides efficient key iteration without creating unnecessary copies.

# Create a dictionary
config = {'debug': True, 'environment': 'production', 'port': 8080}

# Get and use keys view
keys_view = config.keys()
print(f"Initial keys: {list(keys_view)}")

# Demonstrate dynamic nature of keys view
config['new_setting'] = 'value'
print(f"Updated keys: {list(keys_view)}")

# Check key existence
print(f"Is 'debug' present?: {'debug' in keys_view}")

# Output:
# Initial keys: ['debug', 'environment', 'port']
# Updated keys: ['debug', 'environment', 'port', 'new_setting']
# Is 'debug' present?: True

Slide 7: Dictionary Pop Method

The pop() method removes and returns the value associated with a specified key. If the key is not found, it returns the default value provided. This method is crucial for safely removing dictionary entries while capturing their values.

# Initialize dictionary
settings = {'theme': 'dark', 'volume': 80, 'notifications': True}

# Remove and get value with pop
theme = settings.pop('theme', 'light')
missing_value = settings.pop('missing_key', 'default')

print(f"Removed theme: {theme}")
print(f"Missing value: {missing_value}")
print(f"Updated settings: {settings}")

# Output:
# Removed theme: dark
# Missing value: default
# Updated settings: {'volume': 80, 'notifications': True}

Slide 8: Dictionary PopItem Method

The popitem() method removes and returns the last inserted key-value pair as a tuple. In Python 3.7+, dictionaries maintain insertion order, making this method predictable for tracking dictionary modifications.

# Create an ordered dictionary (Python 3.7+)
cache = {'first': 1, 'second': 2, 'third': 3}

# Remove items using popitem()
try:
    while True:
        item = cache.popitem()
        print(f"Removed: {item}")
except KeyError:
    print("Dictionary is empty")

# Output:
# Removed: ('third', 3)
# Removed: ('second', 2)
# Removed: ('first', 1)
# Dictionary is empty

Slide 9: Dictionary SetDefault Method

setdefault() retrieves the value of a specified key and sets it with a default value if the key doesn't exist. This method provides an atomic operation for checking and setting dictionary values, improving code efficiency.

# Initialize counter dictionary
word_count = {}

# Count words using setdefault
text = "apple banana apple cherry banana apple"
for word in text.split():
    word_count.setdefault(word, 0)
    word_count[word] += 1

print(f"Word frequencies: {word_count}")

# Using setdefault with complex default values
users = {}
new_user = users.setdefault('john', {'name': 'John', 'posts': []})
print(f"New user entry: {new_user}")

# Output:
# Word frequencies: {'apple': 3, 'banana': 2, 'cherry': 1}
# New user entry: {'name': 'John', 'posts': []}

Slide 10: Dictionary Update Method

The update() method merges one dictionary into another, overwriting existing keys with new values. This method accepts various input formats including dictionaries, key-value pairs, and keyword arguments.

# Base configuration
config = {'host': 'localhost', 'port': 8080}

# Update with another dictionary
config.update({'port': 9000, 'debug': True})
print(f"After dict update: {config}")

# Update with key-value pairs
config.update([('timeout', 30), ('retries', 3)])
print(f"After pairs update: {config}")

# Update with keyword arguments
config.update(ssl=True, max_connections=100)
print(f"After kwargs update: {config}")

# Output:
# After dict update: {'host': 'localhost', 'port': 9000, 'debug': True}
# After pairs update: {'host': 'localhost', 'port': 9000, 'debug': True, 'timeout': 30, 'retries': 3}
# After kwargs update: {'host': 'localhost', 'port': 9000, 'debug': True, 'timeout': 30, 'retries': 3, 'ssl': True, 'max_connections': 100}

Slide 11: Dictionary Values Method

The values() method returns a dynamic view of dictionary values. This view object provides efficient iteration over dictionary values and automatically reflects any changes made to the original dictionary.

# Create a metrics dictionary
metrics = {'cpu': 45.2, 'memory': 82.1, 'disk': 56.8}

# Get values view
values_view = metrics.values()
print(f"Initial values: {list(values_view)}")

# Calculate statistics using values
avg_usage = sum(values_view) / len(values_view)
print(f"Average usage: {avg_usage:.2f}")

# Demonstrate dynamic nature
metrics['network'] = 91.5
print(f"Updated values: {list(values_view)}")

# Output:
# Initial values: [45.2, 82.1, 56.8]
# Average usage: 61.37
# Updated values: [45.2, 82.1, 56.8, 91.5]

Slide 12: Dictionary Real-World Example - Cache Implementation

A practical implementation of a dictionary as a cache system with size limits and automatic cleanup of least recently used items. This pattern is commonly used in web applications and database query caching.

class LRUCache:
    def __init__(self, capacity: int):
        self.cache = {}
        self.capacity = capacity
        self.usage_count = {}
    
    def get(self, key: str) -> str:
        if key in self.cache:
            self.usage_count[key] += 1
            return self.cache[key]
        return None
    
    def put(self, key: str, value: str) -> None:
        # Remove least used item if cache is full
        if len(self.cache) >= self.capacity and key not in self.cache:
            least_used = min(self.usage_count.items(), key=lambda x: x[1])[0]
            self.cache.pop(least_used)
            self.usage_count.pop(least_used)
        
        self.cache[key] = value
        self.usage_count[key] = 1

# Usage example
cache = LRUCache(3)
cache.put('user_1', 'John Doe')
cache.put('user_2', 'Jane Smith')
cache.put('user_3', 'Bob Johnson')

print(f"Cache content: {cache.cache}")
print(f"Getting user_1: {cache.get('user_1')}")

# Output:
# Cache content: {'user_1': 'John Doe', 'user_2': 'Jane Smith', 'user_3': 'Bob Johnson'}
# Getting user_1: John Doe

Slide 13: Dictionary Real-World Example - Configuration Manager

Implementation of a configuration management system using dictionaries, demonstrating nested structures, default values, and environment-specific settings commonly used in production applications.

class ConfigManager:
    def __init__(self):
        self.config = {
            'development': {
                'database': {
                    'host': 'localhost',
                    'port': 5432,
                    'credentials': {
                        'username': 'dev_user',
                        'password': 'dev_pass'
                    }
                },
                'cache': {
                    'enabled': True,
                    'ttl': 300
                }
            },
            'production': {
                'database': {
                    'host': 'prod.db.server',
                    'port': 5432,
                    'credentials': {
                        'username': 'prod_user',
                        'password': 'prod_pass'
                    }
                },
                'cache': {
                    'enabled': True,
                    'ttl': 3600
                }
            }
        }
    
    def get_setting(self, env: str, *keys, default=None):
        current = self.config.get(env, {})
        for key in keys:
            if isinstance(current, dict):
                current = current.get(key, default)
            else:
                return default
        return current

# Usage example
config = ConfigManager()
db_host = config.get_setting('development', 'database', 'host')
cache_ttl = config.get_setting('production', 'cache', 'ttl')

print(f"Development DB Host: {db_host}")
print(f"Production Cache TTL: {cache_ttl}")

# Output:
# Development DB Host: localhost
# Production Cache TTL: 3600

Slide 14: Additional Resources