Skip to content

Commit 0d555f2

Browse files
authored
Merge pull request #954 from kris-joseph/gh-pages
Updated nested list example in episode 4
2 parents 00a4d83 + aacd92f commit 0d555f2

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

_episodes/04-lists.md

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,57 +166,80 @@ does not.
166166
> ## Nested Lists
167167
> Since a list can contain any Python variables, it can even contain other lists.
168168
>
169-
> For example, we could represent the products in the shelves of a small grocery shop:
169+
> For example, you could represent the products on the shelves of a small grocery shop
170+
> as a nested list called `veg`:
171+
>
172+
> ![`veg` is represented as a shelf full of produce. There are three rows of vegetables
173+
> on the shelf, and each row contains three baskets of vegetables. We can label
174+
> each basket according to the type of vegetable it contains, so the top row
175+
> contains (from left to right) lettuce, lettuce, and peppers.](../fig/04_groceries_veg.png)
176+
>
177+
> To store the contents of the shelf in a nested list, you write it this way:
170178
>
171179
> ~~~
172-
> x = [['pepper', 'zucchini', 'onion'],
173-
> ['cabbage', 'lettuce', 'garlic'],
174-
> ['apple', 'pear', 'banana']]
180+
> veg = [['lettuce', 'lettuce', 'peppers', 'zucchini'],
181+
> ['lettuce', 'lettuce', 'peppers', 'zucchini'],
182+
> ['lettuce', 'cilantro', 'peppers', 'zucchini']]
175183
> ~~~
176184
> {: .language-python}
177185
>
178-
> Here is a visual example of how indexing a list of lists `x` works:
186+
> Here are some visual examples of how indexing a list of lists `veg` works. First,
187+
> you can reference each row on the shelf as a separate list. For example, `veg[2]`
188+
> represents the bottom row, which is a list of the baskets in that row.
179189
>
180-
> [![x is represented as a pepper shaker containing several packets of pepper. [x[0]] is represented
181-
> as a pepper shaker containing a single packet of pepper. x[0] is represented as a single packet of
182-
> pepper. x[0][0] is represented as single grain of pepper. Adapted
183-
> from @hadleywickham.](../fig/indexing_lists_python.png)][hadleywickham-tweet]
190+
> ![`veg` is now shown as a list of three rows, with `veg[0]` representing the top row of
191+
> three baskets, `veg[1]` representing the second row, and `veg[2]` representing the bottom row.](../fig/04_groceries_veg0.png)
184192
>
185-
> Using the previously declared list `x`, these would be the results of the
186-
> index operations shown in the image:
193+
> Index operations using the image would work like this:
187194
>
188195
> ~~~
189-
> print([x[0]])
196+
> print(veg[2])
190197
> ~~~
191198
> {: .language-python}
192199
>
193200
> ~~~
194-
> [['pepper', 'zucchini', 'onion']]
201+
> ['lettuce', 'cilantro', 'peppers', 'zucchini']
195202
> ~~~
196203
> {: .output}
197204
>
198205
> ~~~
199-
> print(x[0])
206+
> print(veg[0])
207+
> ~~~
208+
> {: .language-python}
209+
>
210+
> ~~~
211+
> ['lettuce', 'lettuce', 'peppers', 'zucchini']
212+
> ~~~
213+
> {: .output}
214+
>
215+
> To reference a specific basket on a specific shelf, you use two indexes. The first
216+
> index represents the row (from top to bottom) and the second index represents
217+
> the specific basket (from left to right).
218+
> ![`veg` is now shown as a two-dimensional grid, with each basket labeled according to
219+
> its index in the nested list. The first index is the row number and the second
220+
> index is the basket number, so `veg[1][3]` represents the basket on the far right
221+
> side of the second row (basket 4 on row 2): zucchini](../fig/04_groceries_veg00.png)
222+
>
223+
> ~~~
224+
> print(veg[0][0])
200225
> ~~~
201226
> {: .language-python}
202227
>
203228
> ~~~
204-
> ['pepper', 'zucchini', 'onion']
229+
> 'lettuce'
205230
> ~~~
206231
> {: .output}
207232
>
208233
> ~~~
209-
> print(x[0][0])
234+
> print(veg[1][2])
210235
> ~~~
211236
> {: .language-python}
212237
>
213238
> ~~~
214-
> 'pepper'
239+
> 'peppers'
215240
> ~~~
216241
> {: .output}
217242
>
218-
> Thanks to [Hadley Wickham][hadleywickham-tweet]
219-
> for the image above.
220243
{: .callout}
221244
222245
> ## Heterogeneous Lists

fig/04_groceries_veg.png

710 KB
Loading

fig/04_groceries_veg0.png

671 KB
Loading

fig/04_groceries_veg00.png

564 KB
Loading

0 commit comments

Comments
 (0)