Skip to content

Commit ff6905e

Browse files
committed
change comprehension code into jupyter notebooks
1 parent 2f9096c commit ff6905e

13 files changed

+460
-113
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# List Comprehension\n",
8+
"This directory is here to show the Speed Difference between using \n",
9+
"List Comprehension vs. calling append() in a For Loop to create a list\n",
10+
"\n",
11+
"## Executing\n",
12+
"Standard append loop:\n",
13+
"\n",
14+
"```python\n",
15+
"l2 = []\n",
16+
"for i in range(100000000):\n",
17+
"\tl.append(i)\n",
18+
"```\n",
19+
"\n",
20+
"In a UNIX terminal, run:\n",
21+
"```bash\n",
22+
"time python list-append-loop.py\n",
23+
"\n",
24+
"real\t0m11.381s\n",
25+
"user\t0m10.553s\n",
26+
"sys\t0m0.824s\n",
27+
"```\n",
28+
"\n",
29+
"List comprehension:\n",
30+
"\n",
31+
"```python\n",
32+
"l1 = [x for x in range(100000000)]\n",
33+
"```\n",
34+
"\n",
35+
"In a UNIX terminal, run:\n",
36+
"```bash\n",
37+
"time python list-comp.py\n",
38+
"\n",
39+
"real\t0m4.228s\n",
40+
"user\t0m3.428s\n",
41+
"sys\t0m0.800s\n",
42+
"```\n",
43+
"\n",
44+
"\n",
45+
"Here we see that the list comprehension was almost 3x as fast\n",
46+
"as the `list.append()` for loop. This is because everytime the\n",
47+
"append() method is called, it has to be looked up by the Interpreter,\n",
48+
"whereas the list comprehension can do it all at once.\n",
49+
"\n",
50+
"### Disclaimer\n",
51+
"Since this is a very simple example where we are just creating a list of\n",
52+
"numbers with the range() function, we can actually just cast the result\n",
53+
"of the range() function to a list and get even faster speed than the\n",
54+
"list comprehension. \n",
55+
"\n",
56+
"```python\n",
57+
"l = list(range(100000000))\n",
58+
"```\n",
59+
"\n",
60+
"In a UNIX terminal, run:\n",
61+
"```\n",
62+
"time python cast-list.py\n",
63+
"\n",
64+
"real\t0m2.988s\n",
65+
"user\t0m2.176s\n",
66+
"sys\t0m0.812s\n",
67+
"```\n",
68+
"\n",
69+
"To show a more practical example where you can't just cast it:\n",
70+
"\n",
71+
"More complicated example with append loop:\n",
72+
"```python\n",
73+
"l2 = []\n",
74+
"for i in range(1000000):\n",
75+
"\tl.append(i**2)\n",
76+
"```\n",
77+
"\n",
78+
"In a UNIX terminal, run:\n",
79+
"```\n",
80+
"time python list-append-loop2.py\n",
81+
"\n",
82+
"real\t0m29.756s\n",
83+
"user\t0m28.875s\n",
84+
"sys\t0m0.880s\n",
85+
"```\n",
86+
"\n",
87+
"Same thing with list comprehension:\n",
88+
"```python\n",
89+
"l1 = [x**2 for x in range(1000000)]\n",
90+
"```\n",
91+
"\n",
92+
"In a UNIX terminal, run:\n",
93+
"\n",
94+
"```\n",
95+
"time python list-comp2.py\n",
96+
"\n",
97+
"real\t0m21.932s\n",
98+
"user\t0m21.031s\n",
99+
"sys\t0m0.900s\n",
100+
"```\n",
101+
"\n",
102+
"In this practical example, we see that the list comprehension finished\n",
103+
"in about 2/3 of the time it took the for loop append() method, still\n",
104+
"significantly faster!\n",
105+
"\n",
106+
"All in all, it depends on the example, but list comprehensions are often\n",
107+
"faster than using a loop.\n"
108+
]
109+
}
110+
],
111+
"metadata": {
112+
"kernelspec": {
113+
"display_name": "Python 3",
114+
"language": "python",
115+
"name": "python3"
116+
},
117+
"language_info": {
118+
"codemirror_mode": {
119+
"name": "ipython",
120+
"version": 3
121+
},
122+
"file_extension": ".py",
123+
"mimetype": "text/x-python",
124+
"name": "python",
125+
"nbconvert_exporter": "python",
126+
"pygments_lexer": "ipython3",
127+
"version": "3.6.4"
128+
}
129+
},
130+
"nbformat": 4,
131+
"nbformat_minor": 2
132+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Dictionary Comprehension\n",
8+
"\n",
9+
"Other than list comprehension, and for comprehensions in general, speed isn't the primary concern.\n",
10+
"Comprehensions are mostly used for code conciseness and readability. Here is an example of a typical way that you would create a dictionary:"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 3,
16+
"metadata": {},
17+
"outputs": [
18+
{
19+
"name": "stdout",
20+
"output_type": "stream",
21+
"text": [
22+
"{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}\n"
23+
]
24+
}
25+
],
26+
"source": [
27+
"d = {}\n",
28+
"\n",
29+
"# Map every number from 1-9 to it's perfect square\n",
30+
"for i in range(1, 10):\n",
31+
"\td[i] = i**2\n",
32+
" \n",
33+
"print(d)"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"But this can be done in just 1 line of code with dict comprehension:"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 6,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}\n"
53+
]
54+
}
55+
],
56+
"source": [
57+
"d2 = {i : i**2 for i in range(1, 10)}\n",
58+
"print(d2)"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"Since this is basically the same concept as list comprehension, as well as set/tuple comprehensions, I don't\n",
66+
"include any more examples."
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": []
75+
}
76+
],
77+
"metadata": {
78+
"kernelspec": {
79+
"display_name": "Python 3",
80+
"language": "python",
81+
"name": "python3"
82+
},
83+
"language_info": {
84+
"codemirror_mode": {
85+
"name": "ipython",
86+
"version": 3
87+
},
88+
"file_extension": ".py",
89+
"mimetype": "text/x-python",
90+
"name": "python",
91+
"nbconvert_exporter": "python",
92+
"pygments_lexer": "ipython3",
93+
"version": "3.6.4"
94+
}
95+
},
96+
"nbformat": 4,
97+
"nbformat_minor": 2
98+
}

comprehension/dictionary/README.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

comprehension/dictionary/dict-comp.py

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Dictionary Comprehension\n",
8+
"\n",
9+
"Other than list comprehension, and for comprehensions in general, speed isn't the primary concern.\n",
10+
"Comprehensions are mostly used for code conciseness and readability. Here is an example of a typical way that you would create a dictionary:"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 3,
16+
"metadata": {},
17+
"outputs": [
18+
{
19+
"name": "stdout",
20+
"output_type": "stream",
21+
"text": [
22+
"{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}\n"
23+
]
24+
}
25+
],
26+
"source": [
27+
"d = {}\n",
28+
"\n",
29+
"# Map every number from 1-9 to it's perfect square\n",
30+
"for i in range(1, 10):\n",
31+
"\td[i] = i**2\n",
32+
" \n",
33+
"print(d)"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"But this can be done in just 1 line of code with dict comprehension:"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 6,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}\n"
53+
]
54+
}
55+
],
56+
"source": [
57+
"d2 = {i : i**2 for i in range(1, 10)}\n",
58+
"print(d2)"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"Since this is basically the same concept as list comprehension, as well as set/tuple comprehensions, I don't\n",
66+
"include any more examples."
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": []
75+
}
76+
],
77+
"metadata": {
78+
"kernelspec": {
79+
"display_name": "Python 3",
80+
"language": "python",
81+
"name": "python3"
82+
},
83+
"language_info": {
84+
"codemirror_mode": {
85+
"name": "ipython",
86+
"version": 3
87+
},
88+
"file_extension": ".py",
89+
"mimetype": "text/x-python",
90+
"name": "python",
91+
"nbconvert_exporter": "python",
92+
"pygments_lexer": "ipython3",
93+
"version": "3.6.4"
94+
}
95+
},
96+
"nbformat": 4,
97+
"nbformat_minor": 2
98+
}

comprehension/dictionary/dict-loop.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)