@@ -141,7 +141,7 @@ Below are some ways to update dictionaries without mutating them:
141141 # Python >= 3.9
142142 d | {"key": value}
143143
144- # Equivalent to setdefault()
144+ # Equivalent to dict. setdefault()
145145 {"key": value, **d}
146146
147147 .. card :: Removing Items
@@ -165,6 +165,9 @@ Below are some ways to update dictionaries without mutating them:
165165 }
166166
167167
168+ ----
169+
170+
168171.. _updating-dictionary-items :
169172
170173Updating Dictionary Items
@@ -293,11 +296,10 @@ Below are some ways to update lists without mutating them:
293296
294297 l[:start] + values + l[end + 1:]
295298
296- ..card:: Re-ordering Items
299+ .. card :: Re-ordering Items
297300 :link: re-ordering-list-items
298301 :link-type: ref
299302
300-
301303 Avoid using ``list.sort `` or ``list.reverse ``. Instead try the strategies below:
302304
303305 .. code-block ::
@@ -307,6 +309,9 @@ Below are some ways to update lists without mutating them:
307309 list(reversed(l))
308310
309311
312+ ----
313+
314+
310315.. _inserting-list-items :
311316
312317Inserting List Items
@@ -324,6 +329,9 @@ Inserting List Items
324329
325330 l.insert(index, value)
326331
332+ # Adding a list "in-place" mutates!
333+ l += [value]
334+
327335 .. grid-item-card :: :bdg-info:`Prefer`
328336
329337 .. code-block ::
@@ -336,6 +344,11 @@ Inserting List Items
336344
337345 l[:index] + [value] + l[index:]
338346
347+ Instead of mutating a list to add items to it, we need to create a new list which has
348+ the items we want to append instead. There are several ways to do this for one or more
349+ values however it's often simplest to use `"unpacking"
350+ <https://www.python.org/dev/peps/pep-0448/> `__ with the ``* `` syntax.
351+
339352.. idom :: _examples/list_insert
340353
341354
@@ -358,7 +371,12 @@ Removing List Items
358371
359372 .. code-block ::
360373
361- l[:index - 1] + l[index:]
374+ l[:index] + l[index + 1:]
375+
376+ Unfortunately, the syntax for creating a copy of a list with one of its items removed is
377+ not quite as clean. You must select the portion the list prior to the item which should
378+ be removed (``l[:index] ``) and the portion after the item (``l[index + 1:] ``) and add
379+ them together:
362380
363381.. idom :: _examples/list_remove
364382
@@ -386,6 +404,11 @@ Replacing List Items
386404
387405 l[:start] + values + l[end + 1:]
388406
407+ In a similar manner to :ref: `removing list items `, to replace an item in a list, you
408+ must select the portion before and after the item in question. But this time, instead
409+ of adding those two selections together, you must insert that values you want to replace
410+ between them:
411+
389412.. idom :: _examples/list_replace
390413
391414
@@ -412,6 +435,12 @@ Re-ordering List Items
412435
413436 list(reversed(l))
414437
438+ There are many different ways that list items could be re-ordered, but two of the most
439+ common are reversing or sorting items. Instead of calling the associated methods on a
440+ list object, you should use the builtin functions :func: `sorted ` and :func: `reversed `
441+ and pass the resulting iterator into the :class: `list ` constructor to create a sorted
442+ or reversed copy of the given list:
443+
415444.. idom :: _examples/list_re_order
416445
417446
@@ -448,6 +477,9 @@ Below are ways to update sets without mutating them:
448477 s.intersection(values)
449478
450479
480+ ----
481+
482+
451483.. _adding-set-items :
452484
453485Adding Set Items
@@ -460,16 +492,27 @@ Adding Set Items
460492 .. code-block ::
461493
462494 s.add(value)
495+ s |= {value} # "in-place" operators mutate!
463496
464497 s.update(values)
498+ s |= values # "in-place" operators mutate!
465499
466500 .. grid-item-card :: :bdg-info:`Prefer`
467501
468502 .. code-block ::
469503
470504 s.union({value})
505+ s | {value}
471506
472507 s.union(values)
508+ s | values
509+
510+ Sets have some nice ways for evolving them without requiring mutation. The binary
511+ or operator ``| `` serves as a succinct way to compute the union of two sets. However,
512+ you should be careful to not use an in-place assignment with this operator as that will
513+ (counterintuitively) mutate the original set rather than creating a new one.
514+
515+ .. idom :: _examples/set_update
473516
474517
475518.. _removing-set-items :
@@ -486,8 +529,14 @@ Removing Set Items
486529 s.remove(value)
487530
488531 s.difference_update(values)
532+ s -= values # "in-place" operators mutate!
533+
534+ s.symetric_difference_update(values)
535+ s ^= values # "in-place" operators mutate!
489536
490537 s.intersection_update(values)
538+ s &= values # "in-place" operators mutate!
539+
491540
492541 .. grid-item-card :: :bdg-info:`Prefer`
493542
@@ -496,10 +545,25 @@ Removing Set Items
496545 s.difference({value})
497546
498547 s.difference(values)
548+ s - values
549+
550+ s.symetric_difference(values)
551+ s ^ values
499552
500553 s.intersection(values)
554+ s & values
555+
556+ To remove items from sets you can use the various binary operators or their associated
557+ methods to return new sets without mutating them. As before when :ref: `adding items to
558+ sets` you need to avoid using the inline assignment operators since that will
559+ (counterintuitively) mutate the original set rather than given you a new one:
560+
561+ .. idom :: _examples/set_remove
501562
502563
503564Useful Packages
504565---------------
505566
567+ Under construction 🚧
568+
569+ https://pypi.org/project/pyrsistent/
0 commit comments