{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## CS 200: List Comprehensions\n", "\n", "\n", "\n", "\n", "### Video:\n", "\n", "See List Comprehensions from Socratica.\n", "\n", "List comprehensions are consise ways of executing functions on lists (and sets and dicts). They are inspired by mathematical notation and the Haskell programming language.\n", "\n", "In math, the common ways to describe lists (or sets, or tuples, or vectors) are: \n", "\n", "
filter
instead of map
\n",
"\n",
"Uses if
inside list comprehension."
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"f = []\n",
"for x in range(10):\n",
" if x % 2 == 0:\n",
" f.append(x)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 4, 6, 8]"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"f1 = list(filter (lambda x: x % 2 == 0, range(10)))"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 4, 6, 8]"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f1"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"f2 = [x for x in range(10) if x % 2 == 0]"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 4, 6, 8]"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f2"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f == f1 == f2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Filters range of integers, selecting the ones greater than 4"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"g = []\n",
"for x in range(10):\n",
" if x > 4:\n",
" g.append(x)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 6, 7, 8, 9]"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"g1 = list(filter (lambda x: x > 4, range(10)))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 6, 7, 8, 9]"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g1"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"g2 = [x for x in range(10) if x > 4]"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 6, 7, 8, 9]"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g2"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"l = ['morse', 'stiles', '', 'franklin', 'edwards','', \"\", '', 'branford', '', 'trumbull', '', '', 'hopper']"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['morse',\n",
" 'stiles',\n",
" '',\n",
" 'franklin',\n",
" 'edwards',\n",
" '',\n",
" '',\n",
" '',\n",
" 'branford',\n",
" '',\n",
" 'trumbull',\n",
" '',\n",
" '',\n",
" 'hopper']"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(l)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['morse', 'stiles', 'franklin', 'edwards', 'branford', 'trumbull', 'hopper']"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(filter(None, l))"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['morse', 'stiles', 'franklin', 'edwards', 'branford', 'trumbull', 'hopper']"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x for x in l if x]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### reduce combines values in a list"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"h = 0\n",
"for x in range(101):\n",
" h += x"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5050"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"from functools import reduce"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"h1 = reduce(lambda x, y: x+y, range(101))"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5050"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h1"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h == h1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generating prime numbers\n",
"\n",
"See https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"noprimes = [j for i in range(2, 8) for j in range(i*2, 100, i)]"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4,\n",
" 6,\n",
" 8,\n",
" 10,\n",
" 12,\n",
" 14,\n",
" 16,\n",
" 18,\n",
" 20,\n",
" 22,\n",
" 24,\n",
" 26,\n",
" 28,\n",
" 30,\n",
" 32,\n",
" 34,\n",
" 36,\n",
" 38,\n",
" 40,\n",
" 42,\n",
" 44,\n",
" 46,\n",
" 48,\n",
" 50,\n",
" 52,\n",
" 54,\n",
" 56,\n",
" 58,\n",
" 60,\n",
" 62,\n",
" 64,\n",
" 66,\n",
" 68,\n",
" 70,\n",
" 72,\n",
" 74,\n",
" 76,\n",
" 78,\n",
" 80,\n",
" 82,\n",
" 84,\n",
" 86,\n",
" 88,\n",
" 90,\n",
" 92,\n",
" 94,\n",
" 96,\n",
" 98,\n",
" 6,\n",
" 9,\n",
" 12,\n",
" 15,\n",
" 18,\n",
" 21,\n",
" 24,\n",
" 27,\n",
" 30,\n",
" 33,\n",
" 36,\n",
" 39,\n",
" 42,\n",
" 45,\n",
" 48,\n",
" 51,\n",
" 54,\n",
" 57,\n",
" 60,\n",
" 63,\n",
" 66,\n",
" 69,\n",
" 72,\n",
" 75,\n",
" 78,\n",
" 81,\n",
" 84,\n",
" 87,\n",
" 90,\n",
" 93,\n",
" 96,\n",
" 99,\n",
" 8,\n",
" 12,\n",
" 16,\n",
" 20,\n",
" 24,\n",
" 28,\n",
" 32,\n",
" 36,\n",
" 40,\n",
" 44,\n",
" 48,\n",
" 52,\n",
" 56,\n",
" 60,\n",
" 64,\n",
" 68,\n",
" 72,\n",
" 76,\n",
" 80,\n",
" 84,\n",
" 88,\n",
" 92,\n",
" 96,\n",
" 10,\n",
" 15,\n",
" 20,\n",
" 25,\n",
" 30,\n",
" 35,\n",
" 40,\n",
" 45,\n",
" 50,\n",
" 55,\n",
" 60,\n",
" 65,\n",
" 70,\n",
" 75,\n",
" 80,\n",
" 85,\n",
" 90,\n",
" 95,\n",
" 12,\n",
" 18,\n",
" 24,\n",
" 30,\n",
" 36,\n",
" 42,\n",
" 48,\n",
" 54,\n",
" 60,\n",
" 66,\n",
" 72,\n",
" 78,\n",
" 84,\n",
" 90,\n",
" 96,\n",
" 14,\n",
" 21,\n",
" 28,\n",
" 35,\n",
" 42,\n",
" 49,\n",
" 56,\n",
" 63,\n",
" 70,\n",
" 77,\n",
" 84,\n",
" 91,\n",
" 98]"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"noprimes"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"primes = [x for x in range(2, 100) if x not in noprimes]"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2,\n",
" 3,\n",
" 5,\n",
" 7,\n",
" 11,\n",
" 13,\n",
" 17,\n",
" 19,\n",
" 23,\n",
" 29,\n",
" 31,\n",
" 37,\n",
" 41,\n",
" 43,\n",
" 47,\n",
" 53,\n",
" 59,\n",
" 61,\n",
" 67,\n",
" 71,\n",
" 73,\n",
" 79,\n",
" 83,\n",
" 89,\n",
" 97]"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"primes"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"149"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(noprimes)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"noprimes contains duplicates. If we convert it to a set, we will eliminate the duplicates."
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3, 4, 5, 6, 7}"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set([1,2,3,4,1,2,3,4,5,6,7])"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"setnoprimes = set(noprimes)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{4,\n",
" 6,\n",
" 8,\n",
" 9,\n",
" 10,\n",
" 12,\n",
" 14,\n",
" 15,\n",
" 16,\n",
" 18,\n",
" 20,\n",
" 21,\n",
" 22,\n",
" 24,\n",
" 25,\n",
" 26,\n",
" 27,\n",
" 28,\n",
" 30,\n",
" 32,\n",
" 33,\n",
" 34,\n",
" 35,\n",
" 36,\n",
" 38,\n",
" 39,\n",
" 40,\n",
" 42,\n",
" 44,\n",
" 45,\n",
" 46,\n",
" 48,\n",
" 49,\n",
" 50,\n",
" 51,\n",
" 52,\n",
" 54,\n",
" 55,\n",
" 56,\n",
" 57,\n",
" 58,\n",
" 60,\n",
" 62,\n",
" 63,\n",
" 64,\n",
" 65,\n",
" 66,\n",
" 68,\n",
" 69,\n",
" 70,\n",
" 72,\n",
" 74,\n",
" 75,\n",
" 76,\n",
" 77,\n",
" 78,\n",
" 80,\n",
" 81,\n",
" 82,\n",
" 84,\n",
" 85,\n",
" 86,\n",
" 87,\n",
" 88,\n",
" 90,\n",
" 91,\n",
" 92,\n",
" 93,\n",
" 94,\n",
" 95,\n",
" 96,\n",
" 98,\n",
" 99}"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"setnoprimes"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"73"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(setnoprimes)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Set comprehensions\n",
"\n",
"By using {} instead of [] we can create set comprehensions."
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"s1 = [x if x % 2 else x + 1 for x in range(10)]"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 1, 3, 3, 5, 5, 7, 7, 9, 9]"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s1"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
"s2 = {x if x % 2 else x + 1 for x in range(10)}"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 3, 5, 7, 9}"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can create literal sets using {}"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
"s3 = {1, 2, 3, 1, 2, 3}"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3}"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s3"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"noprimesset = {j for i in range(2, 8) for j in range(i*2, 100, i)}"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{4,\n",
" 6,\n",
" 8,\n",
" 9,\n",
" 10,\n",
" 12,\n",
" 14,\n",
" 15,\n",
" 16,\n",
" 18,\n",
" 20,\n",
" 21,\n",
" 22,\n",
" 24,\n",
" 25,\n",
" 26,\n",
" 27,\n",
" 28,\n",
" 30,\n",
" 32,\n",
" 33,\n",
" 34,\n",
" 35,\n",
" 36,\n",
" 38,\n",
" 39,\n",
" 40,\n",
" 42,\n",
" 44,\n",
" 45,\n",
" 46,\n",
" 48,\n",
" 49,\n",
" 50,\n",
" 51,\n",
" 52,\n",
" 54,\n",
" 55,\n",
" 56,\n",
" 57,\n",
" 58,\n",
" 60,\n",
" 62,\n",
" 63,\n",
" 64,\n",
" 65,\n",
" 66,\n",
" 68,\n",
" 69,\n",
" 70,\n",
" 72,\n",
" 74,\n",
" 75,\n",
" 76,\n",
" 77,\n",
" 78,\n",
" 80,\n",
" 81,\n",
" 82,\n",
" 84,\n",
" 85,\n",
" 86,\n",
" 87,\n",
" 88,\n",
" 90,\n",
" 91,\n",
" 92,\n",
" 93,\n",
" 94,\n",
" 95,\n",
" 96,\n",
" 98,\n",
" 99}"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"noprimesset"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"73"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(noprimesset)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionary comprehensions"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
"d = {i : x*x for (i,x) in enumerate(range(10))}"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"64"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d[8]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"End of list comprehension notebook"
]
}
],
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}