Dictionary Operations
Complete reference for Python dictionary methods and operations. Dictionaries are mutable, unordered collections of key-value pairs that provide fast lookup and flexible data storage capabilities.
| Operation | Syntax | Example | Result | Description |
|---|
| Empty Dictionary | {} or dict() | my_dict = {} | {} | Create empty dictionary |
| Dictionary Literal | {key: value, ...} | {'a': 1, 'b': 2} | {'a': 1, 'b': 2} | Create with key-value pairs |
| Dict Constructor | dict(key=value, ...) | dict(a=1, b=2) | {'a': 1, 'b': 2} | Create using constructor |
| From Sequences | dict(sequence) | dict([('a', 1), ('b', 2)]) | {'a': 1, 'b': 2} | Create from key-value pairs |
| Dictionary Comprehension | {k: v for k, v in iterable} | {x: x**2 for x in range(3)} | {0: 0, 1: 1, 2: 4} | Create with expression |
| Length | len(dict) | len({'a': 1, 'b': 2}) | 2 | Get number of key-value pairs |
| Membership | key in dict | 'a' in {'a': 1, 'b': 2} | True | Check if key exists |
| Access Value | dict[key] | {'a': 1, 'b': 2}['a'] | 1 | Get value by key |
| Set Value | dict[key] = value | d['a'] = 1 | Modifies dict | Set key-value pair |
| Delete Item | del dict[key] | del d['a'] | Modifies dict | Remove key-value pair |
| Method | Purpose | Example | Result | Description |
|---|
| keys() | Get all keys | d.keys() | dict_keys view | View of dictionary keys |
| values() | Get all values | d.values() | dict_values view | View of dictionary values |
| items() | Get key-value pairs | d.items() | dict_items view | View of key-value pairs |
| Method | Purpose | Example | Result | Description |
|---|
| copy() | Shallow copy | new_dict = d.copy() | New dict | Creates shallow copy |
| dict() | Copy constructor | new_dict = dict(d) | New dict | Creates shallow copy |
| **{dict} | Dictionary unpacking | new_dict = {**d} | New dict | Creates shallow copy (Python 3.5+) |
| Type | Syntax | Example | Result | Description |
|---|
| Basic | {k: v for k, v in iterable} | {x: x**2 for x in range(3)} | {0: 0, 1: 1, 2: 4} | Create dict from expression |
| With Condition | {k: v for k, v in items if condition} | {k: v for k, v in d.items() if v > 0} | Filtered dict | Filter based on condition |
| Key Transformation | {f(k): v for k, v in items} | {k.upper(): v for k, v in d.items()} | Modified keys | Transform keys |
| Value Transformation | {k: f(v) for k, v in items} | {k: v*2 for k, v in d.items()} | Modified values | Transform values |
| Operation | Average Case | Worst Case | Notes |
|---|
| Access/Set/Delete | O(1) | O(n) | Hash collision can cause O(n) |
| Search (in) | O(1) | O(n) | Checking key existence |
| Iteration | O(n) | O(n) | Must visit all items |
| Copy | O(n) | O(n) | Must copy all items |
# Dictionary memory usage
import sys
# Small dictionary
small_dict = {'a': 1, 'b': 2, 'c': 3}
print(sys.getsizeof(small_dict)) # Memory usage in bytes
# Dictionaries have some overhead
# Consider alternatives for simple cases:
# For fixed keys, consider namedtuple
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
point = Point(1, 2) # More memory efficient than {'x': 1, 'y': 2}
# For many similar dictionaries, consider __slots__
class Person:
__slots__ = ['name', 'age'] # Reduces memory usage
def __init__(self, name, age):
self.name = name
self.age = age