Skip to content

Commit 194f2a0

Browse files
authored
Updated to include case of single element list
Indexing[0] to extract an element nested in a list occurs frequently in code handling JSON and is an extension of this anti-pattern
1 parent b4b7c23 commit 194f2a0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/correctness/not_using_explicit_unpacking.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,28 @@ The modified code below is functionally equivalent to the original code, but thi
3131
elem0, elem1, elem2 = elems
3232
3333
34+
Anti-pattern
35+
------------
36+
37+
Indexing [0] to extract a single data structure nested in a list. It is not clear why the first element is being indexed.
38+
39+
.. code:: python
40+
41+
data = [{"InstanceId": "i-1"}]
42+
43+
instance = data[0]
44+
45+
46+
Best practice
47+
-------------
48+
49+
Use unpacking
50+
.............
51+
52+
The modified code below is functionally equivalent but is explicit.
53+
54+
.. code:: python
55+
56+
data = [{"InstanceId": "i-1"}]
57+
58+
instance, = data

0 commit comments

Comments
 (0)