@@ -129,39 +129,43 @@ does not.
129129> the list value, it will change for both variables!
130130>
131131> ~~~
132- > salsa = ['peppers', 'onions', 'cilantro', 'tomatoes']
133- > my_salsa = salsa # <-- my_salsa and salsa point to the *same* list data in memory
134- > salsa[0] = 'hot peppers'
135- > print('Ingredients in my salsa:', my_salsa)
132+ > mild_salsa = ['peppers', 'onions', 'cilantro', 'tomatoes']
133+ > hot_salsa = mild_salsa # <-- mild_salsa and hot_salsa point to the *same* list data in memory
134+ > hot_salsa[0] = 'hot peppers'
135+ > print('Ingredients in mild salsa:', mild_salsa)
136+ > print('Ingredients in hot salsa:', hot_salsa)
136137> ~~~
137138> {: .language-python}
138139>
139140> ~~~
140- > Ingredients in my salsa: ['hot peppers', 'onions', 'cilantro', 'tomatoes']
141+ > Ingredients in mild salsa: ['hot peppers', 'onions', 'cilantro', 'tomatoes']
142+ > Ingredients in hot salsa: ['hot peppers', 'onions', 'cilantro', 'tomatoes']
141143> ~~~
142144> {: .output}
143145>
144146> If you want variables with mutable values to be independent, you
145147> must make a copy of the value when you assign it.
146148>
147149> ~~~
148- > salsa = ['peppers', 'onions', 'cilantro', 'tomatoes']
149- > my_salsa = list(salsa) # <-- makes a *copy* of the list
150- > salsa[0] = 'hot peppers'
151- > print('Ingredients in my salsa:', my_salsa)
150+ > mild_salsa = ['peppers', 'onions', 'cilantro', 'tomatoes']
151+ > hot_salsa = list(mild_salsa) # <-- makes a *copy* of the list
152+ > hot_salsa[0] = 'hot peppers'
153+ > print('Ingredients in mild salsa:', mild_salsa)
154+ > print('Ingredients in hot salsa:', hot_salsa)
152155> ~~~
153156> {: .language-python}
154157>
155158> ~~~
156- > Ingredients in my salsa: ['peppers', 'onions', 'cilantro', 'tomatoes']
159+ > 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.
@@ -291,7 +295,7 @@ odds after reversing: [11, 7, 5, 3]
291295While modifying in place, it is useful to remember that Python treats lists in a slightly
292296counter-intuitive way.
293297
294- As we saw earlier, when we modified the `salsa ` list item in-place, if we make a list, (attempt to)
298+ As we saw earlier, when we modified the `mild_salsa ` list item in-place, if we make a list, (attempt to)
295299copy it and then modify this list, we can cause all sorts of trouble. This also applies to modifying
296300the list using the above functions:
297301
0 commit comments