Skip to content

Commit dad9378

Browse files
authored
Add clarifying output
Added more output to clarify salsa example differences with copies vs pointers for lists.
1 parent fe80fa5 commit dad9378

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

_episodes/04-lists.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,17 @@ does not.
129129
> the list value, it will change for both variables!
130130
>
131131
> ~~~
132-
> mlld_salsa = ['peppers', 'onions', 'cilantro', 'tomatoes']
132+
> mild_salsa = ['peppers', 'onions', 'cilantro', 'tomatoes']
133133
> hot_salsa = mild_salsa # <-- mild_salsa and hot_salsa point to the *same* list data in memory
134134
> hot_salsa[0] = 'hot peppers'
135135
> print('Ingredients in mild salsa:', mild_salsa)
136+
> print('Ingredients in hot salsa:', hot_salsa)
136137
> ~~~
137138
> {: .language-python}
138139
>
139140
> ~~~
140141
> Ingredients in mild salsa: ['hot peppers', 'onions', 'cilantro', 'tomatoes']
142+
> Ingredients in hot salsa: ['hot peppers', 'onions', 'cilantro', 'tomatoes']
141143
> ~~~
142144
> {: .output}
143145
>
@@ -149,19 +151,21 @@ does not.
149151
> hot_salsa = list(mild_salsa) # <-- makes a *copy* of the list
150152
> hot_salsa[0] = 'hot peppers'
151153
> print('Ingredients in mild salsa:', mild_salsa)
154+
> print('Ingredients in hot salsa:', hot_salsa)
152155
> ~~~
153156
> {: .language-python}
154157
>
155158
> ~~~
156159
> Ingredients in mild salsa: ['peppers', 'onions', 'cilantro', 'tomatoes']
160+
> Ingredients in hot salsa: ['hot peppers', 'onions', 'cilantro', 'tomatoes']
157161
> ~~~
158162
> {: .output}
159163
>
160164
> Because of pitfalls like this, code which modifies data in place can be more difficult to
161165
> understand. However, it is often far more efficient to modify a large data structure in place
162166
> than to create a modified copy for every small change. You should consider both of these aspects
163167
> when writing your code.
164-
{: .callout}
168+
> {: .callout}
165169
166170
> ## Nested Lists
167171
> Since a list can contain any Python variables, it can even contain other lists.

0 commit comments

Comments
 (0)