{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "female-customs",
   "metadata": {},
   "source": [
    "# List Slicing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "korean-management",
   "metadata": {},
   "source": [
    "List slicing is a way to get not just one element of a list, but a whole portion."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "canadian-young",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"]\n",
    "L[2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "optional-february",
   "metadata": {},
   "source": [
    "`L[a:b]` means the portion of the list from index `a` (inclusive) to index `b` (exclusive)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "nasty-miracle",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[2:5] # [L[2], L[3], L[4]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "stylish-dealing",
   "metadata": {},
   "source": [
    "If you leave out `a`, it starts from the beginning of the list. If you leave out `b`, it goes to the end."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "short-charger",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "brown-marketplace",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f49b4ef8-77fb-4723-95c7-a1658179b20c",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(L[:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3aef85aa-d431-4e5c-963b-ef1bba545a8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "R = L[:] # a copy of the list!\n",
    "R = list(L) # another way to do the same thing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73b9ef77",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "deluxe-contributor",
   "metadata": {},
   "source": [
    "You can use a third piece `L[a:b:c]`, and `c` means how much to go up by each time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "mathematical-health",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(L[1:5:2])\n",
    "print([L[1], L[3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fffda418",
   "metadata": {},
   "outputs": [],
   "source": [
    "L = list(range(0, 21))\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8dde5159",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[::3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "individual-design",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fleet-balloon",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb2e6146",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[::-2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "intense-questionnaire",
   "metadata": {},
   "source": [
    "Lastly, you can use negative indexing too. For example, to get the last 3 elements of a list:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc1dd6f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cubic-latest",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[-3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8113eaa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[len(L)-3:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "clinical-stewart",
   "metadata": {},
   "source": [
    "To get all except the last element:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b2db509",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[:len(L)-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "olympic-salad",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f0da356",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9b900ad",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48612df3",
   "metadata": {},
   "outputs": [],
   "source": [
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e3a2dcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[:round(len(L)/2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2fc498e",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[round(len(L)/2):]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfeb5d9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e95a080a",
   "metadata": {},
   "outputs": [],
   "source": [
    "L[5:]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
