Skip to content

Commit de276e6

Browse files
committed
finish setting up the layout of the page
1 parent cfcbc1e commit de276e6

File tree

1 file changed

+103
-13
lines changed
  • docs/source/adding-interactivity/dangers-of-mutability

1 file changed

+103
-13
lines changed

docs/source/adding-interactivity/dangers-of-mutability/index.rst

Lines changed: 103 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Below are some ways to update dictionaries without mutating them:
148148
:link: removing-dictionary-items
149149
:link-type: ref
150150

151-
Avoid using item deletion or ``dict.pop`` instead try the strategies below:
151+
Avoid using item deletion or ``dict.pop``. Instead try the strategies below:
152152

153153
.. code-block::
154154
@@ -252,10 +252,64 @@ you can click a delete button to remove the term and definition:
252252
Working with Lists
253253
------------------
254254

255-
Below are some operations to :bdg-danger:`avoid` and stragies to :bdg-info:`prefer` when
256-
working with lists...
255+
Below are some ways to update lists without mutating them:
256+
257+
.. card:: Replacing Items
258+
:link: replacing-list-items
259+
:link-type: ref
260+
261+
Avoid using item or slice assignment. Instead try the strategies below:
262+
263+
.. code-block::
264+
265+
l[:index] + [value] + l[index + 1:]
266+
267+
l[:start] + values + l[end + 1:]
268+
269+
.. card:: Inserting Items
270+
:link: inserting-list-items
271+
:link-type: ref
272+
273+
Avoid using ``list.append``, ``list.extend``, and ``list.insert``. Instead try the
274+
strategies below:
275+
276+
.. code-block::
277+
278+
[*l, value]
279+
280+
l + [value]
281+
282+
l + values
283+
284+
l[:index] + [value] + l[index:]
285+
286+
.. card:: Removing Items
287+
:link: removing-list-items
288+
:link-type: ref
289+
290+
Avoid using item deletion or ``list.pop``. Instead try the strategy below:
291+
292+
.. code-block::
293+
294+
l[:index - 1] + l[index:]
295+
296+
297+
..card:: Re-ordering Items
298+
:link: re-ordering-list-items
299+
:link-type: ref
300+
301+
302+
Avoid using ``list.sort`` or ``list.reverse``. Instead try the strategies below:
303+
304+
.. code-block::
305+
306+
list(sorted(l))
307+
308+
list(reversed(l))
257309
258310
311+
.. _replacing-list-items:
312+
259313
Replacing List Items
260314
....................
261315

@@ -278,6 +332,8 @@ Replacing List Items
278332
l[:start] + values + l[end + 1:]
279333
280334
335+
.. _inserting-list-items:
336+
281337
Inserting List Items
282338
....................
283339

@@ -306,6 +362,8 @@ Inserting List Items
306362
l[:index] + [value] + l[index:]
307363
308364
365+
.. _removing-list-items:
366+
309367
Removing List Items
310368
...................
311369

@@ -326,6 +384,8 @@ Removing List Items
326384
l[:index - 1] + l[index:]
327385
328386
387+
.. _re-ordering-list-items:
388+
329389
Re-ordering List Items
330390
......................
331391

@@ -351,8 +411,34 @@ Re-ordering List Items
351411
Working with Sets
352412
-----------------
353413

354-
Below are some operations to :bdg-danger:`avoid` and stragies to :bdg-info:`prefer` when
355-
working with sets...
414+
Below are ways to update sets without mutating them:
415+
416+
.. card:: Updating Items
417+
:link: updating-dictionary-sets
418+
:link-type: ref
419+
420+
Avoid using item assignment, ``set.add`` or ``set.update``. Instead try the
421+
strategies below:
422+
423+
.. code-block::
424+
425+
s.union({value})
426+
427+
s.union(values)
428+
429+
.. card:: Removing Items
430+
:link: removing-dictionary-sets
431+
:link-type: ref
432+
433+
Avoid using item deletion or ``dict.pop``. Instead try the strategies below:
434+
435+
.. code-block::
436+
437+
s.difference({value})
438+
439+
s.difference(values)
440+
441+
s.intersection(values)
356442
357443
Adding Set Items
358444
................
@@ -363,17 +449,17 @@ Adding Set Items
363449

364450
.. code-block::
365451
366-
l[index] = value
452+
s.add(value)
367453
368-
l[start:end] = values
454+
s.update(values)
369455
370456
.. grid-item-card:: :bdg-info:`Prefer`
371457

372458
.. code-block::
373459
374-
l[:index] + [value] + l[index + 1:]
460+
s.union({value})
375461
376-
l[:start] + values + l[end + 1:]
462+
s.union(values)
377463
378464
379465
Removing Set Items
@@ -385,17 +471,21 @@ Removing Set Items
385471

386472
.. code-block::
387473
388-
l[index] = value
474+
s.remove(value)
389475
390-
l[start:end] = values
476+
s.difference_update(values)
477+
478+
s.intersection_update(values)
391479
392480
.. grid-item-card:: :bdg-info:`Prefer`
393481

394482
.. code-block::
395483
396-
l[:index] + [value] + l[index + 1:]
484+
s.difference({value})
397485
398-
l[:start] + values + l[end + 1:]
486+
s.difference(values)
487+
488+
s.intersection(values)
399489
400490
401491
Useful Packages

0 commit comments

Comments
 (0)