{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists, Tuples, and Dictionaries\n", "\n", "### Other resources\n", "\n", "Python course at https://codecademy.com\n", "\n", "Python data structures tutorial https://docs.python.org/2/tutorial/datastructures.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists\n", "\n", "Python lists are used to store a collection of data. For example, a collection of integers:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 6, 4]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1, 2, 6, 4]\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or a collection of floating point numbers:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1.1, 3.0]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1.1, 3.0]\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or even heterogeneous data, like an integer and a floating point number and a string:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 1.3, 'string']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1, 1.3, 'string']\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can even have list of lists, and those can be heterogeneous as well" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['a list', 'of strings'], [1, 2, 3]]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [['a list', 'of strings'],[1,2,3]]\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists support indexing to extract the data stored in them." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'of strings'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[0][1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also perform operations on lists, e.g. appending values" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1,2,3]\n", "x.append(4)\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or inserting into the middle of them." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 10, 3, 4]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.insert(2, 10)\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or sorting" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 10]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.sort()\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is a shorthand syntax for appending lists to other lists" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = [['a list', 'of strings'],[1,2,3]]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['a list', 'of strings'], [1, 2, 3], [1.1, 2.2]]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x + [[1.1, 2.2]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use the indexing syntax for reassignment. For example, to replace the first positing of the list `x`, you can use the `O`th index. In Python, counting starts from `0`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['another list'], [1, 2, 3]]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[0] = ['another list']\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists are mutable! Meaning they can be changed, extended, appended, inserted into, sorted, etc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple\n", "\n", "Tuples are kind of like constant list. If you have data that you want to store in a structure, but you know it will not be changed, tuples can be more efficient. The syntax for tuples is given in the example below:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = (1, 2, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is an effiecent \"unpacking\" syntax for tuples." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b, c = x\n", "a" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You cannot reassign values in a tuple. They are immutable!" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\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[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "x[0] = 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary\n", "\n", "Dictionaries are a way to efficient store and retrieve `keyword: value` type data. For example:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "my_dict = {'numerical' : 1, 'alphabetical': 'abc'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we can look up the value of `'numerical'` quickly:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict['numerical']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or `'alphabetical'`" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict['alphabetical']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just likes lists, you can have a dictionary-of-dictionaries" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_dict = {'numerical' : {'linear solver' : 'gmres'}, 'alphabetical': 'abc'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To return the value of `'linear solver'` directly" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'gmres'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict['numerical']['linear solver']" ] } ], "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.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }