{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "module-prog", "module-dsml" ] }, "source": [ "(List_comprehensions)=\n", "# List comprehensions\n", "``` {index} List comprehension (Python)\n", "```\n", "List comprehensions are 'shortcuts' in creating lists using loops within one statement. The main advantage is that they are shorter to write. The syntax is:\n", "\n", "```python\n", " some_list = [expression for item in iterable_object if condition]\n", "```\n", " \n", "This will generate a list with an expression based on items in 'iterable_object', if the condition is met. The `if` condition is optional." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 4, 7, 10, 13, 16, 19]\n", "[1, 64, 343, 1000, 2197, 4096, 6859]\n", "[1, 4, 7, 10, 13, 16, 19]\n", "[1, 64, 343, 1000, 2197, 4096, 6859]\n" ] } ], "source": [ "# List comprehensions\n", "\n", "normal_numbers = [number for number in range(1, 20, 3)]\n", "print(normal_numbers)\n", "\n", "cubic_numbers = [number**3 for number in range(1, 20, 3)]\n", "print(cubic_numbers)\n", "\n", "# These are equivalent to \n", "\n", "normal_numbers = []\n", "cubic_numbers = []\n", "\n", "for number in range(1, 20, 3):\n", " normal_numbers.append(number)\n", " cubic_numbers.append(number**3)\n", " \n", "print(normal_numbers)\n", "print(cubic_numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The expression in list comprehension can be anything, the same value for each item, a string...:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 0, 0, 0, 0]\n", "['text', 'text', 'text', 'text', 'text']\n" ] } ], "source": [ "zeros_list = [ 0 for i in range(5)]\n", "text_list = [\"text\" for i in range(5)]\n", "\n", "print(zeros_list)\n", "print(text_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using `if` condition:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n", "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n" ] } ], "source": [ "# List comprehension with if statement\n", "# number%2 == 1 means to search for numbers whose \n", "# remainder from division by 2 is 1 (odd numbers)\n", "\n", "odd_numbers = [ number for number in range(20) if number%2 == 1]\n", "print(odd_numbers)\n", "\n", "# Equivalent to\n", "\n", "odd_numbers = []\n", "for number in range(20):\n", " if number%2 == 1:\n", " odd_numbers.append(number)\n", " \n", "print(odd_numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(list_comprehensions_exercises)=\n", "## Exercises:\n", "_____________\n", "* **Create a list comprehension** to find Celsius temperatures for Fahrenheit temperatures between $20^\\circ$F and $80^\\circ$F at $5^\\circ$F increments. The Fahrenheit-Celsius conversion formula is:\n", "\n", "\n$$ T_{Celsius} = \\frac{5}{9}*(T_{Fahrenheit}-32). $$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "````{admonition} Answer\n", ":class: dropdown\n", "\n", "```python\n", "temp_C = [ 5./9.*(i-32) for i in range(20, 81, 5)]\n", "\n", "print(temp_C)\n", "```\n", "\n", "````" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **Create a list** of powers of two $2^x$ where $x$ is positive, odd, not divisible by 3 and smaller than 40. Iterate through that list and print the last integer of each element. Numbers tend to show up patterns in such analysis, can you spot one?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "````{admonition} Answer\n", ":class: dropdown\n", "\n", "```python\n", "l = [2**x for x in range(40) if x % 2 == 1 and not(x % 3 == 0)]\n", "\n", "for i in l:\n", " print(i%10) \n", "```\n", "\n", "````" ] } ], "metadata": { "celltoolbar": "Tags", "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.8.8" } }, "nbformat": 4, "nbformat_minor": 4 }