Skip to content

Instantly share code, notes, and snippets.

@ewjoachim
Created September 14, 2015 08:47
Show Gist options
  • Save ewjoachim/3697e2ca9491dfc9c117 to your computer and use it in GitHub Desktop.
Save ewjoachim/3697e2ca9491dfc9c117 to your computer and use it in GitHub Desktop.
Comprehensions
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Loop"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 5, 10, 15, 20]\n"
]
}
],
"source": [
"a = []\n",
"b = range(5)\n",
"for i in b:\n",
" a.append(i * 5)\n",
" \n",
"print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# List Comprehension"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 5, 10, 15, 20]\n"
]
}
],
"source": [
"a = [i * 5 for i in range(5)]\n",
"\n",
"print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Set Comprehension"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"set([0, 10, 20, 5, 15])\n"
]
}
],
"source": [
"a = {i * 5 for i in range(5)}\n",
"\n",
"print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dict Comprehension"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{0: 0, 1: 5, 2: 10, 3: 15, 4: 20}\n"
]
}
],
"source": [
"a = {i: i * 5 for i in range(5)}\n",
"\n",
"print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Double loop"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9)]\n"
]
}
],
"source": [
"a = []\n",
"for i in range(5):\n",
" for j in range(5, 10):\n",
" a.append((i, j))\n",
" \n",
"print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# List Comprehension"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9)]\n"
]
}
],
"source": [
"a = [(i, j) for i in range(5) for j in range(5, 10)]\n",
"\n",
"print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Same splitted into multiple lines"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9)]\n"
]
}
],
"source": [
"a = [\n",
" (i, j)\n",
" for i in range(5)\n",
" for j in range(5, 10)\n",
"]\n",
"\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment