Skip to content

Commit fa1759d

Browse files
committed
finish setting up the layout of the page
1 parent efefd3c commit fa1759d

File tree

1 file changed

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

1 file changed

+108
-13
lines changed

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

Lines changed: 108 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,9 +252,63 @@ 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:
257256

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))
309+
310+
311+
.. _replacing-list-items:
258312

259313
Replacing List Items
260314
....................
@@ -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,37 @@ 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:: Adding Items
417+
:link: adding-set-items
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-set-items
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)
442+
443+
444+
.. _adding-set-items:
356445

357446
Adding Set Items
358447
................
@@ -363,18 +452,20 @@ Adding Set Items
363452

364453
.. code-block::
365454
366-
l[index] = value
455+
s.add(value)
367456
368-
l[start:end] = values
457+
s.update(values)
369458
370459
.. grid-item-card:: :bdg-info:`Prefer`
371460

372461
.. code-block::
373462
374-
l[:index] + [value] + l[index + 1:]
463+
s.union({value})
464+
465+
s.union(values)
375466
376-
l[:start] + values + l[end + 1:]
377467
468+
.. _removing-set-items:
378469

379470
Removing Set Items
380471
..................
@@ -385,17 +476,21 @@ Removing Set Items
385476

386477
.. code-block::
387478
388-
l[index] = value
479+
s.remove(value)
389480
390-
l[start:end] = values
481+
s.difference_update(values)
482+
483+
s.intersection_update(values)
391484
392485
.. grid-item-card:: :bdg-info:`Prefer`
393486

394487
.. code-block::
395488
396-
l[:index] + [value] + l[index + 1:]
489+
s.difference({value})
397490
398-
l[:start] + values + l[end + 1:]
491+
s.difference(values)
492+
493+
s.intersection(values)
399494
400495
401496
Useful Packages

0 commit comments

Comments
 (0)