|
| 1 | +--- |
| 2 | +id: python-dictionaries |
| 3 | +title: Python Dictionaries |
| 4 | +description: Complete theoretical explanation of dictionaries in Python, covering creation, modification, methods, and use-cases. |
| 5 | +sidebar_label: Python Dictionaries #displays in sidebar |
| 6 | +sidebar_position: 11 |
| 7 | +tags: |
| 8 | + [ |
| 9 | + Python, |
| 10 | + Introduction of python, |
| 11 | + List in Python, |
| 12 | + Python Syntax, |
| 13 | + Variables, |
| 14 | + Operators, |
| 15 | + Type Casting, |
| 16 | + String, |
| 17 | + Tuple in Python, |
| 18 | + Python Dictionaries |
| 19 | + |
| 20 | + ] |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | + |
| 25 | +# Python Dictionaries |
| 26 | + |
| 27 | +A **dictionary** in Python is an unordered, mutable, and indexed collection of key-value pairs. It is one of the most powerful and flexible built-in data structures in Python, suitable for representing structured data. |
| 28 | + |
| 29 | +## What is a Dictionary? |
| 30 | + |
| 31 | +Dictionaries hold data in the form of key-value pairs. Each key is unique and maps to a specific value. Values can be of any data type, while keys must be immutable (like strings, numbers, or tuples). |
| 32 | + |
| 33 | +### Example: |
| 34 | +```python |
| 35 | +person = { |
| 36 | + "name": "Alice", |
| 37 | + "age": 25, |
| 38 | + "city": "New York" |
| 39 | +} |
| 40 | +```` |
| 41 | + |
| 42 | +## Properties of Dictionaries |
| 43 | + |
| 44 | +* Keys are unique. |
| 45 | +* Keys must be immutable. |
| 46 | +* Values can be of any data type. |
| 47 | +* Dictionaries are mutable and can be changed after creation. |
| 48 | +* In Python 3.7+, dictionaries maintain insertion order. |
| 49 | + |
| 50 | +## Creating Dictionaries |
| 51 | + |
| 52 | +### Using Curly Braces: |
| 53 | + |
| 54 | +```python |
| 55 | +data = {"a": 1, "b": 2} |
| 56 | +``` |
| 57 | + |
| 58 | +### Using the `dict()` Constructor: |
| 59 | + |
| 60 | +```python |
| 61 | +data = dict(x=10, y=20) |
| 62 | +``` |
| 63 | + |
| 64 | +### Creating an Empty Dictionary: |
| 65 | + |
| 66 | +```python |
| 67 | +empty = {} |
| 68 | +``` |
| 69 | + |
| 70 | +## Accessing Dictionary Elements |
| 71 | + |
| 72 | +### Using Key Indexing: |
| 73 | + |
| 74 | +```python |
| 75 | +person["name"] |
| 76 | +``` |
| 77 | + |
| 78 | +### Using `get()` Method: |
| 79 | + |
| 80 | +```python |
| 81 | +person.get("age") |
| 82 | +person.get("gender", "Not Found") |
| 83 | +``` |
| 84 | + |
| 85 | +## Adding and Updating Items |
| 86 | + |
| 87 | +### Add New Key-Value: |
| 88 | + |
| 89 | +```python |
| 90 | +person["gender"] = "Female" |
| 91 | +``` |
| 92 | + |
| 93 | +### Update Existing Key: |
| 94 | + |
| 95 | +```python |
| 96 | +person["age"] = 30 |
| 97 | +``` |
| 98 | + |
| 99 | +### Use `update()` Method: |
| 100 | + |
| 101 | +```python |
| 102 | +person.update({"age": 35, "city": "Chicago"}) |
| 103 | +``` |
| 104 | + |
| 105 | +## Removing Elements |
| 106 | + |
| 107 | +### Using `pop()`: |
| 108 | + |
| 109 | +```python |
| 110 | +person.pop("age") |
| 111 | +``` |
| 112 | + |
| 113 | +### Using `del`: |
| 114 | + |
| 115 | +```python |
| 116 | +del person["city"] |
| 117 | +``` |
| 118 | + |
| 119 | +### Using `clear()`: |
| 120 | + |
| 121 | +```python |
| 122 | +person.clear() |
| 123 | +``` |
| 124 | + |
| 125 | +### Using `popitem()`: |
| 126 | + |
| 127 | +Removes and returns the last inserted key-value pair. |
| 128 | + |
| 129 | +```python |
| 130 | +person.popitem() |
| 131 | +``` |
| 132 | + |
| 133 | +## Dictionary Methods |
| 134 | + |
| 135 | +| Method | Description | |
| 136 | +| ----------- | ------------------------------------------------ | |
| 137 | +| `get(key)` | Returns value for key or `None` if key not found | |
| 138 | +| `keys()` | Returns a view of all keys | |
| 139 | +| `values()` | Returns a view of all values | |
| 140 | +| `items()` | Returns a view of key-value pairs | |
| 141 | +| `update()` | Updates dictionary with another dictionary | |
| 142 | +| `pop(key)` | Removes specified key | |
| 143 | +| `popitem()` | Removes the last inserted item | |
| 144 | +| `clear()` | Removes all elements | |
| 145 | +| `copy()` | Returns a shallow copy | |
| 146 | + |
| 147 | +## Iterating Through a Dictionary |
| 148 | + |
| 149 | +### Loop Through Keys: |
| 150 | + |
| 151 | +```python |
| 152 | +for key in person: |
| 153 | + print(key) |
| 154 | +``` |
| 155 | + |
| 156 | +### Loop Through Values: |
| 157 | + |
| 158 | +```python |
| 159 | +for value in person.values(): |
| 160 | + print(value) |
| 161 | +``` |
| 162 | + |
| 163 | +### Loop Through Key-Value Pairs: |
| 164 | + |
| 165 | +```python |
| 166 | +for key, value in person.items(): |
| 167 | + print(key, value) |
| 168 | +``` |
| 169 | + |
| 170 | +## Nested Dictionaries |
| 171 | + |
| 172 | +A dictionary can contain other dictionaries as values, enabling hierarchical data storage. |
| 173 | + |
| 174 | +```python |
| 175 | +students = { |
| 176 | + "101": {"name": "John", "grade": "A"}, |
| 177 | + "102": {"name": "Emma", "grade": "B"}, |
| 178 | +} |
| 179 | +students["101"]["name"] # Output: John |
| 180 | +``` |
| 181 | + |
| 182 | +## Dictionary Comprehension |
| 183 | + |
| 184 | +Like list comprehensions, dictionary comprehensions offer a concise way to create dictionaries. |
| 185 | + |
| 186 | +```python |
| 187 | +squares = {x: x*x for x in range(1, 6)} |
| 188 | +``` |
| 189 | + |
| 190 | +## Use Cases of Dictionaries |
| 191 | + |
| 192 | +* Representing JSON or structured data |
| 193 | +* Frequency counting (e.g., word count) |
| 194 | +* Lookup tables |
| 195 | +* Configuration or settings |
| 196 | +* Storing database records in memory |
| 197 | + |
| 198 | +## Dictionary vs List |
| 199 | + |
| 200 | +| Feature | Dictionary | List | |
| 201 | +| ---------- | ------------------------ | ----------------- | |
| 202 | +| Structure | Key-value pairs | Indexed elements | |
| 203 | +| Access | Via key | Via index | |
| 204 | +| Order | Insertion ordered (3.7+) | Ordered | |
| 205 | +| Mutability | Mutable | Mutable | |
| 206 | +| Use Case | Lookup, mapping | Sequence of items | |
| 207 | + |
| 208 | +## Best Practices |
| 209 | + |
| 210 | +* Use `.get()` instead of direct key access to avoid `KeyError`. |
| 211 | +* Use dictionary comprehension for cleaner and more readable code. |
| 212 | +* Use keys that are hashable (e.g., strings, numbers). |
| 213 | +* Use dictionaries for fast lookups and structured data representation. |
| 214 | + |
| 215 | +## Summary |
| 216 | + |
| 217 | +* Dictionaries are one of the most versatile data structures in Python. |
| 218 | +* They store key-value pairs and allow fast retrieval based on keys. |
| 219 | +* Keys must be unique and immutable. |
| 220 | +* Dictionaries support powerful methods for data manipulation and traversal. |
0 commit comments