From 2209d24546ba482a07b7220832fca2a1cc8fdda4 Mon Sep 17 00:00:00 2001 From: Dhrubaraj Pati Date: Thu, 17 Jul 2025 21:15:49 +0530 Subject: [PATCH] Add Python dictionaries documentation Introduced a comprehensive guide on Python dictionaries, covering creation, modification, methods, iteration, and use-cases. Also updated sidebar positions to accommodate the new documentation. Co-Authored-By: Sanjay Viswanathan --- docs/python/python-dictionaries.md | 220 +++++++++++++++++++++++++++++ docs/python/setup-environment.md | 2 +- 2 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 docs/python/python-dictionaries.md diff --git a/docs/python/python-dictionaries.md b/docs/python/python-dictionaries.md new file mode 100644 index 00000000..07c84ab8 --- /dev/null +++ b/docs/python/python-dictionaries.md @@ -0,0 +1,220 @@ +--- +id: python-dictionaries +title: Python Dictionaries +description: Complete theoretical explanation of dictionaries in Python, covering creation, modification, methods, and use-cases. +sidebar_label: Python Dictionaries #displays in sidebar +sidebar_position: 11 +tags: + [ + Python, + Introduction of python, + List in Python, + Python Syntax, + Variables, + Operators, + Type Casting, + String, + Tuple in Python, + Python Dictionaries + + ] + +--- + + +# Python Dictionaries + +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. + +## What is a Dictionary? + +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). + +### Example: +```python +person = { + "name": "Alice", + "age": 25, + "city": "New York" +} +```` + +## Properties of Dictionaries + +* Keys are unique. +* Keys must be immutable. +* Values can be of any data type. +* Dictionaries are mutable and can be changed after creation. +* In Python 3.7+, dictionaries maintain insertion order. + +## Creating Dictionaries + +### Using Curly Braces: + +```python +data = {"a": 1, "b": 2} +``` + +### Using the `dict()` Constructor: + +```python +data = dict(x=10, y=20) +``` + +### Creating an Empty Dictionary: + +```python +empty = {} +``` + +## Accessing Dictionary Elements + +### Using Key Indexing: + +```python +person["name"] +``` + +### Using `get()` Method: + +```python +person.get("age") +person.get("gender", "Not Found") +``` + +## Adding and Updating Items + +### Add New Key-Value: + +```python +person["gender"] = "Female" +``` + +### Update Existing Key: + +```python +person["age"] = 30 +``` + +### Use `update()` Method: + +```python +person.update({"age": 35, "city": "Chicago"}) +``` + +## Removing Elements + +### Using `pop()`: + +```python +person.pop("age") +``` + +### Using `del`: + +```python +del person["city"] +``` + +### Using `clear()`: + +```python +person.clear() +``` + +### Using `popitem()`: + +Removes and returns the last inserted key-value pair. + +```python +person.popitem() +``` + +## Dictionary Methods + +| Method | Description | +| ----------- | ------------------------------------------------ | +| `get(key)` | Returns value for key or `None` if key not found | +| `keys()` | Returns a view of all keys | +| `values()` | Returns a view of all values | +| `items()` | Returns a view of key-value pairs | +| `update()` | Updates dictionary with another dictionary | +| `pop(key)` | Removes specified key | +| `popitem()` | Removes the last inserted item | +| `clear()` | Removes all elements | +| `copy()` | Returns a shallow copy | + +## Iterating Through a Dictionary + +### Loop Through Keys: + +```python +for key in person: + print(key) +``` + +### Loop Through Values: + +```python +for value in person.values(): + print(value) +``` + +### Loop Through Key-Value Pairs: + +```python +for key, value in person.items(): + print(key, value) +``` + +## Nested Dictionaries + +A dictionary can contain other dictionaries as values, enabling hierarchical data storage. + +```python +students = { + "101": {"name": "John", "grade": "A"}, + "102": {"name": "Emma", "grade": "B"}, +} +students["101"]["name"] # Output: John +``` + +## Dictionary Comprehension + +Like list comprehensions, dictionary comprehensions offer a concise way to create dictionaries. + +```python +squares = {x: x*x for x in range(1, 6)} +``` + +## Use Cases of Dictionaries + +* Representing JSON or structured data +* Frequency counting (e.g., word count) +* Lookup tables +* Configuration or settings +* Storing database records in memory + +## Dictionary vs List + +| Feature | Dictionary | List | +| ---------- | ------------------------ | ----------------- | +| Structure | Key-value pairs | Indexed elements | +| Access | Via key | Via index | +| Order | Insertion ordered (3.7+) | Ordered | +| Mutability | Mutable | Mutable | +| Use Case | Lookup, mapping | Sequence of items | + +## Best Practices + +* Use `.get()` instead of direct key access to avoid `KeyError`. +* Use dictionary comprehension for cleaner and more readable code. +* Use keys that are hashable (e.g., strings, numbers). +* Use dictionaries for fast lookups and structured data representation. + +## Summary + +* Dictionaries are one of the most versatile data structures in Python. +* They store key-value pairs and allow fast retrieval based on keys. +* Keys must be unique and immutable. +* Dictionaries support powerful methods for data manipulation and traversal. diff --git a/docs/python/setup-environment.md b/docs/python/setup-environment.md index 6ccfc646..cd4122e8 100644 --- a/docs/python/setup-environment.md +++ b/docs/python/setup-environment.md @@ -2,7 +2,7 @@ id: setup-environment title: Setting up your development environment sidebar_label: Setting up environment -sidebar_position: 11 +sidebar_position: 12 tags: [ html,