{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aa498178",
   "metadata": {},
   "source": [
    "# Part 3: Python Collections - Quick Review for Experienced Developers\n",
    "\n",
    "This document provides a quick reference to Python's advanced and specialized collection types from the `collections` module.\n",
    "\n",
    "---\n",
    "\n",
    "### 1. Sorting: `sort()` vs `sorted()`\n",
    "\n",
    "- **`list.sort()`**: In-place sort. Modifies the original list and returns `None`.\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9df93d39",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [3, 1, 2]\n",
    "  my_list.sort() # my_list is now [1, 2, 3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a2ec6e2",
   "metadata": {},
   "source": [
    "\n",
    "- **`sorted()`**: Built-in function. Returns a *new* sorted list, leaving the original unchanged.\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea8cf7fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "original_list = [3, 1, 2]\n",
    "  new_list = sorted(original_list) # new_list is [1, 2, 3], original_list is unchanged"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "787382e6",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "---\n",
    "\n",
    "### 2. Tuple Unpacking\n",
    "\n",
    "- **Basic Unpacking**:\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9a49531",
   "metadata": {},
   "outputs": [],
   "source": [
    "a, b = (10, 20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "578b4701",
   "metadata": {},
   "source": [
    "\n",
    "- **Extended Unpacking (`*`)**:\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a6d3e29",
   "metadata": {},
   "outputs": [],
   "source": [
    "first, *middle, last = [1, 2, 3, 4, 5]\n",
    "  # first -> 1\n",
    "  # middle -> [2, 3, 4]\n",
    "  # last -> 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be4e549c",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "---\n",
    "\n",
    "### 3. Specialized Collections in `collections`\n",
    "\n",
    "A brief overview of useful classes in the `collections` module.\n",
    "\n",
    "| Class | Description | Common Use Case |\n",
    "| :--- | :--- | :--- |\n",
    "| `Counter` | A `dict` subclass for counting hashable objects. | Counting item frequencies, finding most common items. |\n",
    "| `defaultdict`| A `dict` subclass that calls a factory function for missing keys. | Grouping items into lists or other collections without checking for key existence. |\n",
    "| `deque` | \"Double-ended queue\". Fast appends and pops from both ends. | Implementing queues, stacks, or keeping a list of \"last N\" items. |\n",
    "| `namedtuple` | Factory function for creating tuple subclasses with named fields. | Creating simple, immutable objects with attribute access for better readability. |\n",
    "\n",
    "---\n",
    "\n",
    "### 4. Code Examples\n",
    "\n",
    "- **`Counter`**:\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f64680d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "  counts = Counter(['a', 'b', 'a', 'c', 'a'])\n",
    "  # Counter({'a': 3, 'b': 1, 'c': 1})\n",
    "  print(counts.most_common(1)) # [('a', 3)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8b0ee0a",
   "metadata": {},
   "source": [
    "\n",
    "- **`defaultdict`**:\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d71fb5b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import defaultdict\n",
    "  s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4)]\n",
    "  d = defaultdict(list)\n",
    "  for k, v in s:\n",
    "      d[k].append(v)\n",
    "  # defaultdict(<class 'list'>, {'yellow': [1, 3], 'blue': [2, 4]})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e094b10",
   "metadata": {},
   "source": [
    "\n",
    "- **`deque`**:\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "714069c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import deque\n",
    "  last_five = deque(maxlen=5)\n",
    "  for i in range(10):\n",
    "      last_five.append(i)\n",
    "  # deque([5, 6, 7, 8, 9], maxlen=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78246b62",
   "metadata": {},
   "source": [
    "\n",
    "- **`namedtuple`**:\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0978be25",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import namedtuple\n",
    "  Point = namedtuple('Point', ['x', 'y'])\n",
    "  p = Point(11, y=22)\n",
    "  print(p.x, p.y) # 11 22"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17f8b5e1",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "---\n",
    "For more detailed explanations and examples, please see the main [part_3_python_collections.md](./part_3_python_collections.md) document. "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
