Skip to content

Commit aaf1893

Browse files
committed
Add chapter 23
1 parent 5845dd6 commit aaf1893

File tree

3 files changed

+278
-4
lines changed

3 files changed

+278
-4
lines changed
307 KB
Binary file not shown.
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 23. List of lists\n",
8+
"\n",
9+
"\n",
10+
"## Slicing, nested for loops, and flattening\n",
11+
"\n",
12+
"\n",
13+
"[Learn Python with Jupyter](https://learnpythonwithjupyter.com/) by [Serena Bonaretti](https://sbonaretti.github.io/) \n",
14+
"Narrative license: [CC BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/2.0/). Code license: [GNU-GPL v3](https://www.gnu.org/licenses/gpl-3.0.en.html) \n",
15+
"\n",
16+
"---"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"## 1. Slicing"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"- Given the following list of lists: "
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"animals = [[\"dog\", \"cat\"], [\"cow\", \"sheep\", \"horse\", \"chicken\", \"rabbit\"], [\"panda\", \"elephant\", \"giraffe\", \"penguin\"]]"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"- Print the sub-lists containing pets, farm animals, and wild animals: "
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": null,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"print (animals[0])\n",
56+
"print (animals[1])\n",
57+
"print (animals[2])"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {},
63+
"source": [
64+
"- Print the sub-elements \"cat\", \"rabbit\", and from \"panda\" to \"giraffe\":"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"print (animals[0][1])\n",
74+
"print (animals[1][-1])\n",
75+
"print (animals[2][:3])"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"--- \n",
83+
"## 2. Nested for loops"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"- Given the following list of lists: "
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"sports = [[\"skiing\", \"skating\", \"curling\"], [\"canoeing\", \"cycling\", \"swimming\", \"surfing\"]]"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {},
105+
"source": [
106+
"- Print the sub-list elements one-by-one using a nested for loops through *indices*:"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": null,
112+
"metadata": {},
113+
"outputs": [],
114+
"source": [
115+
"for i in range (len(sports)): \n",
116+
" for j in range (len(sports[i])):\n",
117+
" print (sports[i][j])"
118+
]
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"- Print the sub-list elements one-by-one using a nested for loops through *values*:"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": null,
130+
"metadata": {},
131+
"outputs": [],
132+
"source": [
133+
"for seasonal_sports in sports:\n",
134+
" for sport in seasonal_sports:\n",
135+
" print (sport)"
136+
]
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"metadata": {},
141+
"source": [
142+
"---\n",
143+
"## Flattening"
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"metadata": {},
149+
"source": [
150+
"- Given the following list of lists:"
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": null,
156+
"metadata": {},
157+
"outputs": [],
158+
"source": [
159+
"instruments = [[\"contrabass\", \"cello\", \"clarinet\"],[\"gong\", \"guitar\"],[\"tambourine\", \"trumpet\", \"trombone\", \"triangle\"]]"
160+
]
161+
},
162+
{
163+
"cell_type": "markdown",
164+
"metadata": {},
165+
"source": [
166+
"- Flatten the list using a nested for loop through *indices*:"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": null,
172+
"metadata": {},
173+
"outputs": [],
174+
"source": [
175+
"flat_instruments = []\n",
176+
"for i in range(len(instruments)):\n",
177+
" for j in range (len(instruments[i])):\n",
178+
" flat_instruments.append(instruments[i][j])\n",
179+
"print (flat_instruments)"
180+
]
181+
},
182+
{
183+
"cell_type": "markdown",
184+
"metadata": {},
185+
"source": [
186+
"- Flatten the list using a nested for loop through *elements*:"
187+
]
188+
},
189+
{
190+
"cell_type": "code",
191+
"execution_count": null,
192+
"metadata": {},
193+
"outputs": [],
194+
"source": [
195+
"flat_instruments = []\n",
196+
"for group in instruments:\n",
197+
" for instrument in group:\n",
198+
" flat_instruments.append(instrument)\n",
199+
"print (flat_instruments)"
200+
]
201+
},
202+
{
203+
"cell_type": "markdown",
204+
"metadata": {},
205+
"source": [
206+
"- Flatten the list using a for loop and list concatenation:"
207+
]
208+
},
209+
{
210+
"cell_type": "code",
211+
"execution_count": null,
212+
"metadata": {},
213+
"outputs": [],
214+
"source": [
215+
"flat_instruments = []\n",
216+
"for group in instruments:\n",
217+
" flat_instruments += group\n",
218+
"print (flat_instruments)"
219+
]
220+
},
221+
{
222+
"cell_type": "markdown",
223+
"metadata": {},
224+
"source": [
225+
"- Flatten the list using list comprehension:"
226+
]
227+
},
228+
{
229+
"cell_type": "code",
230+
"execution_count": null,
231+
"metadata": {},
232+
"outputs": [],
233+
"source": [
234+
"instruments = [instrument for group in instruments for instrument in group]\n",
235+
"print (instruments)"
236+
]
237+
},
238+
{
239+
"cell_type": "code",
240+
"execution_count": null,
241+
"metadata": {},
242+
"outputs": [],
243+
"source": []
244+
}
245+
],
246+
"metadata": {
247+
"kernelspec": {
248+
"display_name": "Python 3 (ipykernel)",
249+
"language": "python",
250+
"name": "python3"
251+
},
252+
"language_info": {
253+
"codemirror_mode": {
254+
"name": "ipython",
255+
"version": 3
256+
},
257+
"file_extension": ".py",
258+
"mimetype": "text/x-python",
259+
"name": "python",
260+
"nbconvert_exporter": "python",
261+
"pygments_lexer": "ipython3",
262+
"version": "3.11.4"
263+
},
264+
"widgets": {
265+
"application/vnd.jupyter.widget-state+json": {
266+
"state": {},
267+
"version_major": 2,
268+
"version_minor": 0
269+
}
270+
}
271+
},
272+
"nbformat": 4,
273+
"nbformat_minor": 4
274+
}

index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,12 @@ <h2>About the Book</h2>
235235
<div class="col-lg-12">
236236

237237
<div class="progress">
238-
<span class="skill">BOOK COMPLETION <i class="val">60%</i></span>
238+
<span class="skill">BOOK COMPLETION <i class="val">63%</i></span>
239239
<div class="progress-bar-wrap">
240-
<div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100"></div>
240+
<div class="progress-bar" role="progressbar" aria-valuenow="63" aria-valuemin="0" aria-valuemax="100"></div>
241241
</div>
242242
</div>
243-
<center>Next upload: <b>Chapter 23</b> on <b>December 10</b> </center>
243+
<center>Next upload: <b>Chapter 24</b> on <b>January 28</b> </center>
244244
</div>
245245

246246
</div>
@@ -330,7 +330,7 @@ <h4>PART 6: Focus on lists and for loop </h4>
330330
<ol start="21">
331331
<a href="assets/notebooks/21_list_overview.ipynb" onclick="download"><li>Overview of lists<br> <i>Operations, methods, and tricks</i></li></a>
332332
<a href="assets/notebooks/22_for_overview.ipynb" onclick="download" ><li>More about the for loop<br> <i>Various ways of repeating commands on lists and beyond</i></li></a>
333-
<li>Lists of lists<br> <i>Slicing, nested for loops, and flattening</i></li>
333+
<a href="assets/notebooks/23_list_of_lists.ipynb" onclick="download" ><li>Lists of lists<br> <i>Slicing, nested for loops, and flattening</i></li></a>
334334
</ol>
335335
</div>
336336

0 commit comments

Comments
 (0)