@@ -148,7 +148,7 @@ Below are some ways to update dictionaries without mutating them:
148
148
:link: removing-dictionary-items
149
149
:link-type: ref
150
150
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:
152
152
153
153
.. code-block ::
154
154
@@ -252,10 +252,64 @@ you can click a delete button to remove the term and definition:
252
252
Working with Lists
253
253
------------------
254
254
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))
257
309
258
310
311
+ .. _replacing-list-items :
312
+
259
313
Replacing List Items
260
314
....................
261
315
@@ -278,6 +332,8 @@ Replacing List Items
278
332
l[:start] + values + l[end + 1:]
279
333
280
334
335
+ .. _inserting-list-items :
336
+
281
337
Inserting List Items
282
338
....................
283
339
@@ -306,6 +362,8 @@ Inserting List Items
306
362
l[:index] + [value] + l[index:]
307
363
308
364
365
+ .. _removing-list-items :
366
+
309
367
Removing List Items
310
368
...................
311
369
@@ -326,6 +384,8 @@ Removing List Items
326
384
l[:index - 1] + l[index:]
327
385
328
386
387
+ .. _re-ordering-list-items :
388
+
329
389
Re-ordering List Items
330
390
......................
331
391
@@ -351,8 +411,34 @@ Re-ordering List Items
351
411
Working with Sets
352
412
-----------------
353
413
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)
356
442
357
443
Adding Set Items
358
444
................
@@ -363,17 +449,17 @@ Adding Set Items
363
449
364
450
.. code-block ::
365
451
366
- l[index] = value
452
+ s.add( value)
367
453
368
- l[start:end] = values
454
+ s.update( values)
369
455
370
456
.. grid-item-card :: :bdg-info:`Prefer`
371
457
372
458
.. code-block ::
373
459
374
- l[:index] + [ value] + l[index + 1:]
460
+ s.union({ value})
375
461
376
- l[:start] + values + l[end + 1:]
462
+ s.union( values)
377
463
378
464
379
465
Removing Set Items
@@ -385,17 +471,21 @@ Removing Set Items
385
471
386
472
.. code-block ::
387
473
388
- l[index] = value
474
+ s.remove( value)
389
475
390
- l[start:end] = values
476
+ s.difference_update(values)
477
+
478
+ s.intersection_update(values)
391
479
392
480
.. grid-item-card :: :bdg-info:`Prefer`
393
481
394
482
.. code-block ::
395
483
396
- l[:index] + [ value] + l[index + 1:]
484
+ s.difference({ value})
397
485
398
- l[:start] + values + l[end + 1:]
486
+ s.difference(values)
487
+
488
+ s.intersection(values)
399
489
400
490
401
491
Useful Packages
0 commit comments