{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "397c95d8",
   "metadata": {},
   "source": [
    "# Listas y diccionarios\n",
    "\n",
    "Objetivos: practicar indexado, comprensiones, y los métodos de diccionario `get`, `setdefault`, `items`, `update` y el operador `in`. Refuerza el quiz de listas y diccionarios y prepara el proyecto."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80138946",
   "metadata": {},
   "source": [
    "## Listas: indexar, agregar, rebanar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fd628852",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-29T03:43:06.663459Z",
     "iopub.status.busy": "2026-09-29T03:43:06.663309Z",
     "iopub.status.idle": "2026-09-29T03:43:06.669260Z",
     "shell.execute_reply": "2026-09-29T03:43:06.668649Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 23 [8, 15] len: 5\n",
      "[4, 8, 15, 16, 23, 42]\n"
     ]
    }
   ],
   "source": [
    "numeros = [4, 8, 15, 16, 23]\n",
    "print(numeros[0], numeros[-1], numeros[1:3], \"len:\", len(numeros))\n",
    "numeros.append(42)\n",
    "print(numeros)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "351b3720",
   "metadata": {},
   "source": [
    "## Comprensiones de lista\n",
    "\n",
    "Filtrar y transformar en una línea."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b74f2e9b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-29T03:43:06.670974Z",
     "iopub.status.busy": "2026-09-29T03:43:06.670757Z",
     "iopub.status.idle": "2026-09-29T03:43:06.674059Z",
     "shell.execute_reply": "2026-09-29T03:43:06.673519Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8] [2, 4, 6]\n"
     ]
    }
   ],
   "source": [
    "pares = [n for n in range(10) if n % 2 == 0]\n",
    "dobles = [n * 2 for n in [1, 2, 3]]\n",
    "print(pares, dobles)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d74ff51c",
   "metadata": {},
   "source": [
    "## Diccionarios: acceso seguro y recorrido"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e5aeb845",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-29T03:43:06.674953Z",
     "iopub.status.busy": "2026-09-29T03:43:06.674841Z",
     "iopub.status.idle": "2026-09-29T03:43:06.678852Z",
     "shell.execute_reply": "2026-09-29T03:43:06.678526Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30 N/D\n",
      "True True\n",
      "nombre = Ada\n",
      "edad = 31\n",
      "ciudad = Lima\n"
     ]
    }
   ],
   "source": [
    "persona = {\"nombre\": \"Ada\", \"edad\": 30}\n",
    "print(persona.get(\"edad\"), persona.get(\"ciudad\", \"N/D\"))\n",
    "persona.setdefault(\"ciudad\", \"Lima\")\n",
    "print(\"edad\" in persona, \"pais\" not in persona)\n",
    "persona.update({\"edad\": 31})\n",
    "for clave, valor in persona.items():\n",
    "    print(clave, \"=\", valor)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bebd5df",
   "metadata": {},
   "source": [
    "## Comprensión de diccionario"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "017d23be",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-29T03:43:06.679885Z",
     "iopub.status.busy": "2026-09-29T03:43:06.679765Z",
     "iopub.status.idle": "2026-09-29T03:43:06.683005Z",
     "shell.execute_reply": "2026-09-29T03:43:06.682641Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pan': 2.32, 'leche': 1.74}\n"
     ]
    }
   ],
   "source": [
    "precios = {\"pan\": 2.0, \"leche\": 1.5}\n",
    "con_iva = {p: round(v * 1.16, 2) for p, v in precios.items()}\n",
    "print(con_iva)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dc7a18a",
   "metadata": {},
   "source": [
    "## Combinar listas y diccionarios\n",
    "\n",
    "Una lista de diccionarios representa registros; se resume con un cálculo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "23ef59ea",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-29T03:43:06.684119Z",
     "iopub.status.busy": "2026-09-29T03:43:06.684015Z",
     "iopub.status.idle": "2026-09-29T03:43:06.687019Z",
     "shell.execute_reply": "2026-09-29T03:43:06.686648Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "edad promedio: 37.5\n"
     ]
    }
   ],
   "source": [
    "usuarios = [\n",
    "    {\"nombre\": \"Ada\", \"edad\": 30},\n",
    "    {\"nombre\": \"Linus\", \"edad\": 45},\n",
    "]\n",
    "promedio_edad = sum(u[\"edad\"] for u in usuarios) / len(usuarios)\n",
    "print(\"edad promedio:\", promedio_edad)"
   ]
  }
 ],
 "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.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
