Skip to content

Commit 91f1b2e

Browse files
committed
Create not_using_setdefault_to_initialize_a_dictionary.rst.
1 parent 80b55ba commit 91f1b2e

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Not using ``setdefault()`` to initialize a dictionary
2+
=====================================================
3+
4+
Summary
5+
-------
6+
7+
Rather than checking if a key exists in a dictionary via ``if``/``else`` clauses and then creating the key (if it doesn't exist) or setting the key's value (if it already exists), use ``setdefault()``.
8+
9+
Description
10+
-----------
11+
12+
When initializing a dictionary, it is common to see a module check for the existence of a key and then create the key if it does not exist. Although there is nothing wrong with this, the exact same idea can be accomplished more concisely by using the built-in dictionary method ``setdefault()``.
13+
14+
Examples
15+
----------
16+
17+
Using ``if``/``else`` clauses to create and/or set keys
18+
.......................................................
19+
20+
The module below checks if a key named ``list`` exists in a dictionary called ``dictionary``. If it does not exist, then the module creates the key and then sets its value to an empty list. The module then proceeds to append a value to the list.
21+
22+
Although there is nothing wrong with this code, it is unnecessarily verbose. Later you will see how you can use ``setdefault()`` to accomplish the same idea more concisely.
23+
24+
.. warning:: The code below is an example of an error. Using this code will create bugs in your programs!
25+
26+
.. code:: python
27+
28+
dictionary = {}
29+
30+
if not "list" in dictionary:
31+
dictionary["list"] = []
32+
33+
dictionary["list"].append["list_item"]
34+
35+
Solutions
36+
---------
37+
38+
Use ``setdefault()`` to initialize a dictionary
39+
...............................................
40+
41+
The modified module below uses ``setdefault()`` to initialize the dictionary. When ``setdefault()`` is called, it will check if the key already exists. If it does exist, then ``setdefault()`` does nothing. If the key does not exist, then ``setdefault()`` creates it and sets it to the value specified in the second argument.
42+
43+
.. code:: python
44+
45+
dictionary = {}
46+
47+
list = dictionary.setdefault("list", [])
48+
49+
list.append("list_item")
50+
51+
References
52+
----------
53+
- `Stack Overflow - Use cases for the setdefault dict method <http://stackoverflow.com/questions/3483520/use-cases-for-the-setdefault-dict-method>`_

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy