@@ -160,32 +160,40 @@ does not.
160160> ## Nested Lists
161161> Since a list can contain any Python variables, it can even contain other lists.
162162>
163- > For example, we could represent the products in the shelves of a small grocery shop:
163+ > For example, we could represent the products on the shelves of a small grocery shop:
164+ >
165+ > []
170+ >
171+ > To store the contents of the shelf in a nested list, we write it this way:
164172>
165173> ~~~
166- > x = [['pepper', 'zucchini', 'onion'],
167- > ['cabbage', 'lettuce', 'garlic'],
168- > ['apple', 'pear', 'banana']]
174+ > x = [['lettuce', 'lettuce', 'peppers', 'zucchini', 'squash', 'basil'],
175+ > ['lettuce', 'lettuce', 'peppers', 'zucchini', 'peppers', 'parsley'],
176+ > ['lettuce', 'cilantro', 'peppers', 'zucchini', 'squash', 'spinach'],
177+ > ['cabbage', 'broccoli', 'asparagus', 'zucchini', 'squash', 'cauliflower']]
169178> ~~~
170179> {: .language-python}
171180>
172- > Here is a visual example of how indexing a list of lists `x` works:
181+ > Here are some visual examples of how indexing a list of lists `x` works.
173182>
174- > [![x is represented as a pepper shaker containing several packets of pepper. [x[0]] is represented
175- > as a pepper shaker containing a single packet of pepper. x[0] is represented as a single packet of
176- > pepper. x[0][0] is represented as single grain of pepper. Adapted
177- > from @hadleywickham.](../fig/indexing_lists_python.png)][hadleywickham-tweet]
183+ > [![x is now shown as a list of four rows, with x[0] representing the top row of
184+ > five baskets, x[1] representing the second row, x[2] representing the third row,
185+ > and x[3] reprenting the bottom row.](../fig/04_grovceries_x0.png)]
178186>
179187> Using the previously declared list `x`, these would be the results of the
180- > index operations shown in the image:
188+ > index operations shown in the image. Each row on the shelf is a separate list :
181189>
182190> ~~~
183- > print([x[0] ])
191+ > print(x[2 ])
184192> ~~~
185193> {: .language-python}
186194>
187195> ~~~
188- > [['pepper ', 'zucchini', 'onion'] ]
196+ > ['lettuce ', 'cilantro', 'peppers', ' zucchini', 'squash', 'spinach' ]
189197> ~~~
190198> {: .output}
191199>
@@ -195,22 +203,38 @@ does not.
195203> {: .language-python}
196204>
197205> ~~~
198- > ['pepper ', 'zucchini', 'onion ']
206+ > ['lettuce ', 'lettuce', 'peppers', ' zucchini', 'squash', 'basil ']
199207> ~~~
200208> {: .output}
201209>
210+ > To reference a specific basket on a specific shelf, we use two indexes. The first
211+ > index represents the row (from top to bottom) and the second index represents
212+ > the specific basket (from left to right).
213+ >
214+ [![x is now shown as a two-dimensional grid, with each basket labeled according to
215+ > its index in the nested list. The first index is the row number and the second
216+ > index is the basket number, so x[1][4] represents the basket on the far right
217+ > side of the second row (basket 4 on row 1): parsley](../fig/04_grovceries_x00.png)]
218+ >
202219> ~~~
203220> print(x[0][0])
204221> ~~~
205222> {: .language-python}
206223>
207224> ~~~
208- > 'pepper'
225+ > 'lettuce'
226+ > ~~~
227+ >
228+ > ~~~
229+ > print(x[3][2])
230+ > ~~~
231+ > {: .language-python}
232+ >
233+ > ~~~
234+ > 'asparagus'
209235> ~~~
210236> {: .output}
211237>
212- > Thanks to [Hadley Wickham][hadleywickham-tweet]
213- > for the image above.
214238{: .callout}
215239
216240> ## Heterogeneous Lists
0 commit comments