@@ -119,28 +119,199 @@ you touch or hover over the preview:
119119 depends on it. This is called a “local mutation.” You can even do local mutation
120120 while rendering. Very convenient and completely okay!
121121
122- Python provides a number of mutable built in data types:
123122
124- - :ref: `Dictionaries <working with dictionaries >`
125- - :ref: `Lists <working with lists >`
126- - :ref: `Sets <working with sets >`
123+ Working with Dictionaries
124+ -------------------------
127125
128- Below we suggest a number of strategies for safely working with these types...
126+ Below are some operations to :bdg-danger: `avoid ` and stragies to :bdg-info: `prefer ` when
127+ working with dictionaries...
129128
130129
131- Working with Dictionaries
132- -------------------------
130+ Updating Dictionary Items
131+ .........................
132+
133+ .. grid :: 1 1 2 2
134+ :gutter: 1
135+
136+ .. grid-item-card :: :bdg-danger:`Avoid`
137+
138+ .. code-block ::
139+
140+ d["key"] = value
141+
142+ d.update(key=value)
143+
144+ d.setdefault("key", value)
145+
146+ .. grid-item-card :: :bdg-info:`Prefer`
147+
148+ .. code-block ::
149+
150+ {**d, "key": value}
151+
152+ # Python >= 3.9
153+ d | {"key": value}
154+
155+ # Equivalent to setdefault()
156+ {"key": value, **d}
157+
158+
159+ Removing Dictionary Items
160+ .........................
161+
162+ .. grid :: 1 1 2 2
163+ :gutter: 1
164+
165+ .. grid-item-card :: :bdg-danger:`Avoid`
166+
167+ .. code-block ::
168+
169+ d["key"] = value
170+
171+ d.update(key=value)
133172
134- There are a number of different ways to idiomatically construct dictionaries
173+ d.setdefault("key", value)
174+
175+ .. grid-item-card :: :bdg-info:`Prefer`
176+
177+ .. code-block ::
178+
179+ {
180+ k: v
181+ for k, v in d.items()
182+ if k != "key"
183+ }
135184
136185
137186 Working with Lists
138187------------------
139188
189+ Below are some operations to :bdg-danger: `avoid ` and stragies to :bdg-info: `prefer ` when
190+ working with lists...
191+
192+
193+ Replacing List Items
194+ ....................
195+
196+ .. grid :: 1 1 2 2
197+
198+ .. grid-item-card :: :bdg-danger:`Avoid`
199+
200+ .. code-block ::
201+
202+ l[index] = value
203+
204+ l[start:end] = values
205+
206+ .. grid-item-card :: :bdg-info:`Prefer`
207+
208+ .. code-block ::
209+
210+ l[:index] + [value] + l[index + 1:]
211+
212+ l[:start] + values + l[end + 1:]
213+
214+
215+ Inserting List Items
216+ ....................
217+
218+ .. grid :: 1 1 2 2
219+
220+ .. grid-item-card :: :bdg-danger:`Avoid`
221+
222+ .. code-block ::
223+
224+ l.append(value)
225+
226+ l.extend(values)
227+
228+ l.insert(index, value)
229+
230+ .. grid-item-card :: :bdg-info:`Prefer`
231+
232+ .. code-block ::
233+
234+ l + [value]
235+
236+ l + values
237+
238+ l[:index] + [value] + l[index:]
239+
240+
241+ Removing List Items
242+ ...................
243+
244+ .. grid :: 1 1 2 2
245+
246+ .. grid-item-card :: :bdg-danger:`Avoid`
247+
248+ .. code-block ::
249+
250+ del l[index]
251+
252+ l.pop(index)
253+
254+ l.clear()
255+
256+ .. grid-item-card :: :bdg-info:`Prefer`
257+
258+ .. code-block ::
259+
260+ l[:index - 1] + l[index:]
261+
262+ []
263+
264+
265+ Re-ordering List Items
266+ ......................
267+
268+ .. grid :: 1 1 2 2
269+
270+ .. grid-item-card :: :bdg-danger:`Avoid`
271+
272+ .. code-block ::
273+
274+ l.sort()
275+
276+ l.reverse()
277+
278+ .. grid-item-card :: :bdg-info:`Prefer`
279+
280+ .. code-block ::
281+
282+ list(sorted(l))
283+
284+ list(reversed(l))
285+
140286
141287 Working with Sets
142288-----------------
143289
290+ Below are some operations to :bdg-danger: `avoid ` and stragies to :bdg-info: `prefer ` when
291+ working with sets...
292+
293+ Ading Set Items
294+ ...............
295+
296+ .. grid :: 1 1 2 2
297+
298+ .. grid-item-card :: :bdg-danger:`Avoid`
299+
300+ .. code-block ::
301+
302+ l[index] = value
303+
304+ l[start:end] = values
305+
306+ .. grid-item-card :: :bdg-info:`Prefer`
307+
308+ .. code-block ::
309+
310+ l[:index] + [value] + l[index + 1:]
311+
312+ l[:start] + values + l[end + 1:]
313+
314+
315+ Useful Packages
316+ ---------------
144317
145- Working with Nested Data
146- ------------------------
0 commit comments