{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "449be983",
   "metadata": {},
   "source": [
    "# First checks: duplicate records\n",
    "\n",
    "**Illustrative project · synthetic data**\n",
    "\n",
    "## Goal\n",
    "Check a small list for repeated IDs and preserve the first occurrence of each value. This is a teaching example, not evidence of a real project or business result."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98d15fab",
   "metadata": {},
   "source": [
    "## Setup\n",
    "Python 3; no additional packages are needed to run the cells. The input records are included below.\n",
    "\n",
    "## Steps\n",
    "### 1. Inspect the input\n",
    "These IDs are deliberately repeated. In a real dataset, repeated IDs may represent legitimate events; establish the intended row grain before removing anything."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "493c524f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-30T10:33:55.572582Z",
     "iopub.status.busy": "2026-09-30T10:33:55.572468Z",
     "iopub.status.idle": "2026-09-30T10:33:55.577814Z",
     "shell.execute_reply": "2026-09-30T10:33:55.577096Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input: ['A', 'B', 'A', 'C', 'B']\n",
      "Record count: 5\n"
     ]
    }
   ],
   "source": [
    "records = [\"A\", \"B\", \"A\", \"C\", \"B\"]\n",
    "print(\"Input:\", records)\n",
    "print(\"Record count:\", len(records))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8f9b8a4",
   "metadata": {},
   "source": [
    "### 2. Preserve the first occurrence\n",
    "The dictionary keeps insertion order, so the resulting list follows the order of the original IDs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "03574ac7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-30T10:33:55.579081Z",
     "iopub.status.busy": "2026-09-30T10:33:55.578888Z",
     "iopub.status.idle": "2026-09-30T10:33:55.582635Z",
     "shell.execute_reply": "2026-09-30T10:33:55.582054Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Unique IDs: ['A', 'B', 'C']\n",
      "Repeated entries: 2\n"
     ]
    }
   ],
   "source": [
    "def unique_values(values):\n",
    "    return list(dict.fromkeys(values))\n",
    "\n",
    "clean = unique_values(records)\n",
    "print(\"Unique IDs:\", clean)\n",
    "print(\"Repeated entries:\", len(records) - len(clean))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b90282cf",
   "metadata": {},
   "source": [
    "## Checks\n",
    "Test the expected order, an empty input, and idempotence: cleaning an already-clean list should leave it unchanged."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a854981c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-30T10:33:55.583734Z",
     "iopub.status.busy": "2026-09-30T10:33:55.583605Z",
     "iopub.status.idle": "2026-09-30T10:33:55.586945Z",
     "shell.execute_reply": "2026-09-30T10:33:55.586308Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All three checks passed.\n"
     ]
    }
   ],
   "source": [
    "assert clean == [\"A\", \"B\", \"C\"]\n",
    "assert unique_values([]) == []\n",
    "assert unique_values(clean) == clean\n",
    "print(\"All three checks passed.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0e45c8b",
   "metadata": {},
   "source": [
    "## Next steps\n",
    "The five input entries contain three unique IDs and two repeated entries. Before applying this pattern to a real dataset, define which columns identify a record and whether repeated records should be retained, merged, or removed."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
