@@ -46,6 +46,8 @@ Thus far, all the values we've been working with have been immutable. These incl
4646have not had to consider the consiquences of mutations.
4747
4848
49+ .. _Why Avoid Mutation :
50+
4951Why Avoid Mutation?
5052-------------------
5153
@@ -123,54 +125,104 @@ you touch or hover over the preview:
123125Working with Dictionaries
124126-------------------------
125127
126- Below are some operations to :bdg-danger: `avoid ` and stragies to :bdg-info: `prefer ` when
127- working with dictionaries...
128+ Below are some ways to update dictionaries without mutating them:
129+
130+ .. card :: Updating Items
131+ :link: updating-dictionary-items
132+ :link-type: ref
133+
134+ Avoid using item assignment, ``dict.update ``, or ``dict.setdefault ``. Instead try
135+ the strategies below:
136+
137+ .. code-block ::
138+
139+ {**d, "key": value}
140+
141+ # Python >= 3.9
142+ d | {"key": value}
143+
144+ # Equivalent to setdefault()
145+ {"key": value, **d}
146+
147+ .. card :: Removing Items
148+ :link: removing-dictionary-items
149+ :link-type: ref
150+
151+ Avoid using item deletion or ``dict.pop `` instead try the strategies below:
152+
153+ .. code-block ::
154+
155+ {
156+ k: v
157+ for k, v in d.items()
158+ if k != key
159+ }
160+
161+ # Better for removing multiple items
162+ {
163+ k: d[k]
164+ for k in set(d).difference([key])
165+ }
128166
129167
168+ .. _updating-dictionary-items :
169+
130170Updating Dictionary Items
131171.........................
132172
133- .. grid :: 1 1 2 2
173+ .. grid :: 1 1 1 2
134174 :gutter: 1
135175
136176 .. grid-item-card :: :bdg-danger:`Avoid`
137177
138178 .. code-block ::
139179
140- d[" key" ] = value
180+ d[key] = value
141181
142- d.update(key= value)
182+ d.update({ key: value} )
143183
144- d.setdefault(" key" , value)
184+ d.setdefault(key, value)
145185
146186 .. grid-item-card :: :bdg-info:`Prefer`
147187
148188 .. code-block ::
149189
150- {**d, " key" : value}
190+ {**d, key: value}
151191
152192 # Python >= 3.9
153- d | {" key" : value}
193+ d | {key: value}
154194
155195 # Equivalent to setdefault()
156- {"key": value, **d}
196+ {key: value, **d}
197+
198+ As we saw in an :ref: `earlier example <why avoid mutation >`, instead of mutating
199+ dictionaries to update their items you should instead create a copy that contains the
200+ desired changes.
201+
202+ However, sometimes you may only want to update some of the information in a dictionary
203+ which is held by a state variable. Consider the case below where we have a form for
204+ updating user information with a preview of the currently entered data. We can
205+ accomplish this using `"unpacking" <https://www.python.org/dev/peps/pep-0448/ >`__ with
206+ the ``** `` syntax:
157207
208+ .. idom :: _examples/dict_update
209+
210+
211+ .. _removing-dictionary-items :
158212
159213Removing Dictionary Items
160214.........................
161215
162- .. grid :: 1 1 2 2
216+ .. grid :: 1 1 1 2
163217 :gutter: 1
164218
165219 .. grid-item-card :: :bdg-danger:`Avoid`
166220
167221 .. code-block ::
168222
169- d["key"] = value
170-
171- d.update(key=value)
223+ del d[key]
172224
173- d.setdefault(" key", value )
225+ d.pop( key)
174226
175227 .. grid-item-card :: :bdg-info:`Prefer`
176228
@@ -179,9 +231,23 @@ Removing Dictionary Items
179231 {
180232 k: v
181233 for k, v in d.items()
182- if k != "key"
234+ if k != key
235+ }
236+
237+ # Better for removing multiple items
238+ {
239+ k: d[k]
240+ for k in set(d).difference([key])
183241 }
184242
243+ This scenario doesn't come up very frequently. When it does though, the best way to
244+ remove items from dictionaries is to create a copy of the original, but with a filtered
245+ set of keys. One way to do this is with a dictionary comprehension. The example below
246+ shows an interface where you're able to enter a new term and definition. Once added,
247+ you can click a delete button to remove the term and definition:
248+
249+ .. idom :: _examples/dict_remove
250+
185251
186252Working with Lists
187253------------------
@@ -193,7 +259,7 @@ working with lists...
193259Replacing List Items
194260....................
195261
196- .. grid :: 1 1 2 2
262+ .. grid :: 1 1 1 2
197263
198264 .. grid-item-card :: :bdg-danger:`Avoid`
199265
@@ -215,7 +281,7 @@ Replacing List Items
215281 Inserting List Items
216282....................
217283
218- .. grid :: 1 1 2 2
284+ .. grid :: 1 1 1 2
219285
220286 .. grid-item-card :: :bdg-danger:`Avoid`
221287
@@ -231,6 +297,8 @@ Inserting List Items
231297
232298 .. code-block ::
233299
300+ [*l, value]
301+
234302 l + [value]
235303
236304 l + values
@@ -241,7 +309,7 @@ Inserting List Items
241309 Removing List Items
242310...................
243311
244- .. grid :: 1 1 2 2
312+ .. grid :: 1 1 1 2
245313
246314 .. grid-item-card :: :bdg-danger:`Avoid`
247315
@@ -251,21 +319,17 @@ Removing List Items
251319
252320 l.pop(index)
253321
254- l.clear()
255-
256322 .. grid-item-card :: :bdg-info:`Prefer`
257323
258324 .. code-block ::
259325
260326 l[:index - 1] + l[index:]
261327
262- []
263-
264328
265329 Re-ordering List Items
266330......................
267331
268- .. grid :: 1 1 2 2
332+ .. grid :: 1 1 1 2
269333
270334 .. grid-item-card :: :bdg-danger:`Avoid`
271335
@@ -290,10 +354,32 @@ Working with Sets
290354Below are some operations to :bdg-danger: `avoid ` and stragies to :bdg-info: `prefer ` when
291355working with sets...
292356
293- Ading Set Items
294- ...............
357+ Adding Set Items
358+ ................
359+
360+ .. grid :: 1 1 1 2
361+
362+ .. grid-item-card :: :bdg-danger:`Avoid`
363+
364+ .. code-block ::
365+
366+ l[index] = value
367+
368+ l[start:end] = values
369+
370+ .. grid-item-card :: :bdg-info:`Prefer`
371+
372+ .. code-block ::
373+
374+ l[:index] + [value] + l[index + 1:]
375+
376+ l[:start] + values + l[end + 1:]
377+
378+
379+ Removing Set Items
380+ ..................
295381
296- .. grid :: 1 1 2 2
382+ .. grid :: 1 1 1 2
297383
298384 .. grid-item-card :: :bdg-danger:`Avoid`
299385
0 commit comments