From 8626549b1139bf4d7df85b41cb77e18dce68c707 Mon Sep 17 00:00:00 2001 From: KaChunMa Date: Fri, 19 Sep 2025 12:48:00 +0100 Subject: [PATCH 1/8] Edit defaultdict.md to make it clear that default_factory in the syntax is a function --- .../collections-module/terms/defaultdict/defaultdict.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md index 296ea4b17cb..5eda389765f 100644 --- a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md +++ b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md @@ -21,7 +21,7 @@ In Python, **defaultdict** is a data type that belongs to the [`collections`](ht collections.defaultdict(default_factory) ``` -- `default_factory`: It gives the default value for the dictionary object. +- `default_factory`: It is a **function** that gives the default value for the dictionary object. ## Example From d93f8953f1ff15e0a8577e12335a0b5e57b432c3 Mon Sep 17 00:00:00 2001 From: KaChunMa Date: Fri, 19 Sep 2025 13:08:13 +0100 Subject: [PATCH 2/8] Correct from function to callable --- .../collections-module/terms/defaultdict/defaultdict.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md index 5eda389765f..bfba9a49ced 100644 --- a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md +++ b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md @@ -21,7 +21,7 @@ In Python, **defaultdict** is a data type that belongs to the [`collections`](ht collections.defaultdict(default_factory) ``` -- `default_factory`: It is a **function** that gives the default value for the dictionary object. +- `default_factory`: It is a **callable** (e.g. int, list, set, str or a custom function) that gives the default value for the dictionary object. ## Example From e80bb0a636f7c70e81bdddc24eea46274044dc80 Mon Sep 17 00:00:00 2001 From: KaChunMa Date: Fri, 19 Sep 2025 14:02:41 +0100 Subject: [PATCH 3/8] Add examples of different callables being used --- .../terms/defaultdict/defaultdict.md | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md index bfba9a49ced..82310dfbc40 100644 --- a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md +++ b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md @@ -21,11 +21,11 @@ In Python, **defaultdict** is a data type that belongs to the [`collections`](ht collections.defaultdict(default_factory) ``` -- `default_factory`: It is a **callable** (e.g. int, list, set, str or a custom function) that gives the default value for the dictionary object. +- `default_factory`: It is a **callable** that gives the default value for the dictionary object. ## Example -The following example demonstrates the `defaultdict` data type: +The following example demonstrates the `defaultdict` data type with a custom function as `default_factory`: ```py from collections import defaultdict @@ -51,6 +51,55 @@ Here is the output for the above code: Not Declared ``` +The next example demonstrates the use of other callables as `default_factory`: + +```py +from collections import defaultdict + +intdd = defaultdict(int) +listdd = defaultdict(list) +strdd = defaultdict(str) +setdd = defaultdict(set) + +print(intdd[0]) +print(listdd[0]) +print(strdd[0]) +print(setdd[0]) +``` + +Here is the output of the above code: + +```shell +0 +[] +'' +set() +``` +Moreover, the callable determines the methods that can be used when entering key-value pair into the `defaultdict`, here is an example with `list`: + +```py +from collections import defaultdict + +listdd = defaultdict(list) + +listdd[0].append(1) +# listdd[0] does not exist so it defaults to empty list [], +# then 1 is appended to it + +listdd[1] = 2 +#the empty list [] is replaced by integer 2 here + +print(listdd[0]) +print(listdd[1]) +``` + +Here is the output of the above code: + +```shell +[1] +2 +``` + ## Codebyte Example Run the following codeblock and explore more about the `defaultdict` data type: From 8f59396ab3018231a2a7ecce572a0ef21c56bb73 Mon Sep 17 00:00:00 2001 From: KaChunMa Date: Fri, 19 Sep 2025 14:33:10 +0100 Subject: [PATCH 4/8] Revise writing --- .../terms/defaultdict/defaultdict.md | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md index 82310dfbc40..7dc1054bdc2 100644 --- a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md +++ b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md @@ -25,7 +25,7 @@ collections.defaultdict(default_factory) ## Example -The following example demonstrates the `defaultdict` data type with a custom function as `default_factory`: +The following example demonstrates the `defaultdict` data type with a custom function as `default_factory` argument: ```py from collections import defaultdict @@ -51,20 +51,20 @@ Here is the output for the above code: Not Declared ``` -The next example demonstrates the use of other callables as `default_factory`: +The next example demonstrates the use of other callables as the `default_factory` argument: ```py from collections import defaultdict -intdd = defaultdict(int) -listdd = defaultdict(list) -strdd = defaultdict(str) -setdd = defaultdict(set) +intDefaultDict = defaultdict(int) +listDefaultDict = defaultdict(list) +strDefaultDict = defaultdict(str) +setDefaultDict = defaultdict(set) -print(intdd[0]) -print(listdd[0]) -print(strdd[0]) -print(setdd[0]) +print(intDefaultDict[0]) +print(listDefaultDict['zero']) +print(strDefaultDict['0']) +print(setDefaultDict['a']) ``` Here is the output of the above code: @@ -75,22 +75,22 @@ Here is the output of the above code: '' set() ``` -Moreover, the callable determines the methods that can be used when entering key-value pair into the `defaultdict`, here is an example with `list`: +Moreover, the callable determines the methods that can be used when entering a key-value pair into the `defaultdict` data type, here is an example with `list`: ```py from collections import defaultdict -listdd = defaultdict(list) +myDefaultDict = defaultdict(list) -listdd[0].append(1) -# listdd[0] does not exist so it defaults to empty list [], +myDefaultDict['apple'].append(1) +# myDefaultDict['apple'] does not exist so it defaults to empty list [], # then 1 is appended to it -listdd[1] = 2 +myDefaultDict['orange'] = 2 #the empty list [] is replaced by integer 2 here -print(listdd[0]) -print(listdd[1]) +print(myDefaultDict['apple']) +print(myDefaultDict['orange']) ``` Here is the output of the above code: From 9c0799c08866cc5434d2ba0432d2caab3013c546 Mon Sep 17 00:00:00 2001 From: KaChunMa Date: Fri, 19 Sep 2025 14:37:39 +0100 Subject: [PATCH 5/8] Test code and fix output --- .../collections-module/terms/defaultdict/defaultdict.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md index 7dc1054bdc2..77e305fc4b1 100644 --- a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md +++ b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md @@ -72,7 +72,7 @@ Here is the output of the above code: ```shell 0 [] -'' + set() ``` Moreover, the callable determines the methods that can be used when entering a key-value pair into the `defaultdict` data type, here is an example with `list`: From 9cb3825c14c29d47e274e27ddab7686afb4200ef Mon Sep 17 00:00:00 2001 From: KaChunMa Date: Fri, 19 Sep 2025 15:12:34 +0100 Subject: [PATCH 6/8] Further revision --- .../collections-module/terms/defaultdict/defaultdict.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md index 77e305fc4b1..d2bacdc5272 100644 --- a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md +++ b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md @@ -21,7 +21,7 @@ In Python, **defaultdict** is a data type that belongs to the [`collections`](ht collections.defaultdict(default_factory) ``` -- `default_factory`: It is a **callable** that gives the default value for the dictionary object. +- `default_factory`: It is a **callable** (an object that can be called, like a custom function) that gives the default value for the dictionary object. ## Example @@ -84,10 +84,10 @@ myDefaultDict = defaultdict(list) myDefaultDict['apple'].append(1) # myDefaultDict['apple'] does not exist so it defaults to empty list [], -# then 1 is appended to it +# then 1 is appended to it. myDefaultDict['orange'] = 2 -#the empty list [] is replaced by integer 2 here +#The empty list [] is replaced by integer 2 here. print(myDefaultDict['apple']) print(myDefaultDict['orange']) From 48de39dedba33f218c1b5591d67471489c2e884a Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 23 Sep 2025 16:58:24 +0530 Subject: [PATCH 7/8] Update defaultdict.md --- .../terms/defaultdict/defaultdict.md | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md index d2bacdc5272..8d2b8f42609 100644 --- a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md +++ b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md @@ -1,19 +1,19 @@ --- Title: 'defaultdict' -Description: 'Returns a dictionary-like object.' +Description: 'Creates a dictionary-like object that provides default values for missing keys.' Subjects: - 'Computer Science' - 'Data Science' Tags: - 'Modules' - - 'Dictionaries' - 'Data Types' + - 'Dictionaries' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -In Python, **defaultdict** is a data type that belongs to the [`collections`](https://www.codecademy.com/resources/docs/python/collections-module) module. It is a [dictionary](https://www.codecademy.com/resources/docs/python/dictionaries) subclass that is used to return a dictionary-like object. +In Python, **defaultdict** is a data type that belongs to the [`collections`](https://www.codecademy.com/resources/docs/python/collections-module) module. It is a [dictionary](https://www.codecademy.com/resources/docs/python/dictionaries) subclass that automatically provides a default value for missing keys. ## Syntax @@ -21,9 +21,15 @@ In Python, **defaultdict** is a data type that belongs to the [`collections`](ht collections.defaultdict(default_factory) ``` -- `default_factory`: It is a **callable** (an object that can be called, like a custom function) that gives the default value for the dictionary object. +**Parameters:** + +- `default_factory`: A callable (such as a function or type like `int`, `list`, `set`) that provides the default value for missing keys. If set to `None`, a `KeyError` is raised when accessing a missing key. + +**Return value:** + +Returns a `defaultdict` object, which behaves like a dictionary but creates default values for missing keys using the specified `default_factory`. -## Example +## Example 1: Using a Custom Function The following example demonstrates the `defaultdict` data type with a custom function as `default_factory` argument: @@ -51,7 +57,9 @@ Here is the output for the above code: Not Declared ``` -The next example demonstrates the use of other callables as the `default_factory` argument: +Example 2: Using Built-in Callables + +This example demonstrates `defaultdict` with built-in types (`int`, `list`, `str`, `set`) as the `default_factory`: ```py from collections import defaultdict @@ -75,7 +83,10 @@ Here is the output of the above code: set() ``` -Moreover, the callable determines the methods that can be used when entering a key-value pair into the `defaultdict` data type, here is an example with `list`: + +Example 3: Working with Lists + +This example shows how `list` as `default_factory` allows appending to keys that don’t yet exist: ```py from collections import defaultdict @@ -88,7 +99,7 @@ myDefaultDict['apple'].append(1) myDefaultDict['orange'] = 2 #The empty list [] is replaced by integer 2 here. - + print(myDefaultDict['apple']) print(myDefaultDict['orange']) ``` From 5b045c6910623f2626d489f5b9029c4d76b4d7f3 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 23 Sep 2025 16:59:57 +0530 Subject: [PATCH 8/8] Update defaultdict.md --- .../collections-module/terms/defaultdict/defaultdict.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md index 8d2b8f42609..796245db32b 100644 --- a/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md +++ b/content/python/concepts/collections-module/terms/defaultdict/defaultdict.md @@ -13,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -In Python, **defaultdict** is a data type that belongs to the [`collections`](https://www.codecademy.com/resources/docs/python/collections-module) module. It is a [dictionary](https://www.codecademy.com/resources/docs/python/dictionaries) subclass that automatically provides a default value for missing keys. +In Python, **`defaultdict`** is a data type that belongs to the [`collections`](https://www.codecademy.com/resources/docs/python/collections-module) module. It is a [dictionary](https://www.codecademy.com/resources/docs/python/dictionaries) subclass that automatically provides a default value for missing keys. ## Syntax