{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "\n", "\n", "___\n", "
Content Copyright by Pierian Data
" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# List Comprehensions\n", "\n", "In addition to sequence operations and list methods, Python includes a more advanced operation called a list comprehension.\n", "\n", "List comprehensions allow us to build out lists using a different notation. You can think of it as essentially a one line for loop built inside of brackets. For a simple example:\n", "## Example 1" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Grab every letter in string\n", "lst = [x for x in 'word']" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['w', 'o', 'r', 'd']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the basic idea of a list comprehension. If you're familiar with mathematical notation this format should feel familiar for example: x^2 : x in { 0,1,2...10 } \n", "\n", "Let's see a few more examples of list comprehensions in Python:\n", "## Example 2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Square numbers in range and turn into list\n", "lst = [x**2 for x in range(0,11)]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 3\n", "Let's see how to add in if statements:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check for even numbers in a range\n", "lst = [x for x in range(11) if x % 2 == 0]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 2, 4, 6, 8, 10]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 4\n", "Can also do more complicated arithmetic:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[32.0, 50.0, 68.18, 94.1]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Convert Celsius to Fahrenheit\n", "celsius = [0,10,20.1,34.5]\n", "\n", "fahrenheit = [((9/5)*temp + 32) for temp in celsius ]\n", "\n", "fahrenheit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5\n", "We can also perform nested list comprehensions, for example:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lst = [ x**2 for x in [x**2 for x in range(11)]]\n", "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Later on in the course we will learn about generator comprehensions. After this lecture you should feel comfortable reading and writing basic list comprehensions." ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 1 }