{ "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": 2, "id": "nasty-miracle", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['c', 'd', 'e']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[2:5]" ] }, { "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": 3, "id": "short-charger", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'd']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[:4]" ] }, { "cell_type": "code", "execution_count": 4, "id": "brown-marketplace", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['b', 'c', 'd', 'e', 'f']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[1:]" ] }, { "cell_type": "code", "execution_count": 5, "id": "plain-matthew", "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": 13, "id": "de0bf479", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c', 'd', 'e', 'f']\n", "['a', 'b', 'c', 'd', 'e', 'f']\n" ] } ], "source": [ "L = ['a', 'b', 'c', 'd', 'e', 'f']\n", "R = list(L)\n", "print(L)\n", "print(R)" ] }, { "cell_type": "code", "execution_count": 14, "id": "8f1d03c8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['b', 'c', 'd', 'e', 'f']\n", "['a', 'b', 'c', 'd', 'e', 'f']\n" ] } ], "source": [ "R.pop(0)\n", "print(R)\n", "print(L)" ] }, { "cell_type": "code", "execution_count": 9, "id": "8036217a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['b', 'c', 'd', 'e', 'f']\n" ] } ], "source": [ "print(L)" ] }, { "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": 17, "id": "mathematical-health", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['b', 'd']\n", "['b', 'd']\n" ] } ], "source": [ "print(L[1:5:2])\n", "print([L[1], L[3]])" ] }, { "cell_type": "code", "execution_count": 18, "id": "fffda418", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n" ] } ], "source": [ "L = list(range(0, 21))\n", "print(L)" ] }, { "cell_type": "code", "execution_count": 19, "id": "8dde5159", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 3, 6, 9, 12, 15, 18]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[::3]" ] }, { "cell_type": "code", "execution_count": null, "id": "individual-design", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 20, "id": "fleet-balloon", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[::-1]" ] }, { "cell_type": "code", "execution_count": 21, "id": "eb2e6146", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "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": 22, "id": "fc1dd6f7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[-3]" ] }, { "cell_type": "code", "execution_count": 23, "id": "cubic-latest", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[18, 19, 20]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[-3:]" ] }, { "cell_type": "code", "execution_count": 24, "id": "8113eaa4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[18, 19, 20]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "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": 25, "id": "2b2db509", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[:len(L)-1]" ] }, { "cell_type": "code", "execution_count": 26, "id": "olympic-salad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "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": 27, "id": "48612df3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L" ] }, { "cell_type": "code", "execution_count": 29, "id": "3e3a2dcc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[:round(len(L)/2)]" ] }, { "cell_type": "code", "execution_count": 30, "id": "d2fc498e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[round(len(L)/2):]" ] }, { "cell_type": "code", "execution_count": 35, "id": "bfeb5d9f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[:5]" ] }, { "cell_type": "code", "execution_count": 36, "id": "e95a080a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[5:]" ] }, { "cell_type": "code", "execution_count": 33, "id": "1df5ad64", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "23 // 5" ] }, { "cell_type": "code", "execution_count": 34, "id": "267d9ba6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(23/5)" ] }, { "cell_type": "code", "execution_count": null, "id": "af026316", "metadata": {}, "outputs": [], "source": [] } ], "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.11.1" } }, "nbformat": 4, "nbformat_minor": 5 }