{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists\n", "\n", "Lists can be thought of as the most general version of a *sequence* in Python.\n", "Lists are MUTABLE, meaning the elements inside a list can be changed." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# To assing a list on a variable name my_list\n", "my_list = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# ^ list of intergers\n", "my_list = [1, 'some string', 3, 3.5, 'another string']" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'some string', 3, 3.5, 'another string']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(my_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing and Slicing\n", "indexing and slicing works just like strings." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "my_list = ['one', 'two', 'three', 4.5]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# i want the element at index 0\n", "my_list[0]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['two', 'three', 4.5]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# slicing...grab index 1 and everything after it\n", "my_list[1:]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'three']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# grab everything UPTO index 3\n", "my_list[:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use + to concatenate lists, just like strings." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'three', 4.5, 'new item']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list + ['new item']" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['one', 'two', 'three', 4.5, 'new item']\n" ] } ], "source": [ "print(my_list + ['new item'])" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'three', 4.5]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "# reassing my_list\n", "my_list = my_list + ['new item']" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'three', 4.5, 'new item']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use the * for a duplication method similar to string:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one',\n", " 'two',\n", " 'three',\n", " 4.5,\n", " 'new item',\n", " 'one',\n", " 'two',\n", " 'three',\n", " 4.5,\n", " 'new item']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# make the list double\n", "my_list * 2" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'three', 4.5, 'new item']" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic List Method\n", "\n", "Mistly similar Java array, but Python lists are more flexible than array in other Languages for 2 good reasons:\n", "1. They have no fixed size (we dont specify how big a list will be)\n", "2. They have no fixed type constraint.\n", "\n", "Let see some special list methods:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# create a new list\n", "list1 = [1,2,3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we can use **append** method to permanenetly add an item to the end of a list:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "# append\n", "list1.append('appended item')" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'appended item']" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# show\n", "list1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use **pop** to pop off an item from the list. By default, pop take off the last index, but you can specify which index to pop off. Eg," ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pop off the 0 index item\n", "list1.pop(0)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 'appended item']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list1" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "# assing the popped element, default popped index is -1\n", "popped_item = list1.pop()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'appended item'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "popped_item" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# show remaining list\n", "list1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It should be noted that lists indexing will return error if there is no element at the index. For eg," ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlist1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "list1[100]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use the **sort** method and the **reverse** methods to affect your lists:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "new = ['a', 'b', 'x', 'c']" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'x', 'c']" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# show\n", "new" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "# use reverse - this is PERMANENT!\n", "new.reverse()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['c', 'x', 'b', 'a']" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "# sort by alpha. order, for number in asc. order\n", "new.sort()" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'x']" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Nesting Lists\n", "\n", "A special Python feature, supports nesting. This means we can have data structures within data structures. Eg. list inside list" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "# Let's make three lists\n", "lst_1=[1,2,3]\n", "lst_2=[4,5,6]\n", "lst_3=[7,8,9]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "# Make a list of lists to form a matrix\n", "matrix = [lst_1,lst_2,lst_3]" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# show\n", "matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can again use indexig. but now there are two level for the index. The items in the matrix object, and then the items inside that list" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "# Grab first item in matrix object\n", "matrix[0]" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab first item of the first item in the matrix object\n", "matrix[0][0]" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix[1][1]" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix[1][2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# List Comprehensions\n", "\n", "Python advanced feature. Allow for quick construction of lists." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "# builds a list comprehension by deconstructing a for loop within a []\n", "first_col = [row[0] for row in matrix]" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 7]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_col" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 6, 9]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last_col = [i[-1] for i in matrix]\n", "last_col" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }