GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Defaultdict in Python - GeeksforGeeks Defaultdict in PythonLast Updated : 19 May, 2026 Defaultdict is a subclass of the built-in dict class from the collections module. It automatically assigns a default value to keys that do not exist which means no need to manually check for missing keys and avoid KeyError. This example shows how a defaultdict automatically creates missing keys with a default empty list.
Output defaultdict(<class 'list'>, {'fruits': ['apple'], 'vegetables': ['carrot']})
[]
Explanation:
Syntax
Parameters:
Return Value: It returns a dictionary-like object that automatically supplies a default value for missing keys instead of raising KeyError. Why do we need defaultdict()In a normal dictionary, accessing a missing key raises a KeyError. defaultdict solves this by:
How Does defaultdict Work?When you create a defaultdict, you specify a default_factory (a callable).
For example:
This mechanism avoids errors and makes code simpler when handling missing keys. Use Cases of defaultdict1. Using List as Default FactoryWhen the list class is passed as the default_factory argument, a defaultdict is created in which the values are lists. Example: This example shows how we can use list as the default factory, so every missing key will automatically start with an empty list.
Output Dictionary with values as list:
defaultdict(<class 'list'>, {0: [0], 1: [1], 2: [2], 3: [3], 4: [4]})
Explanation: A defaultdict is created with list, which means any missing key will automatically have an empty list as its value. The loop appends the value of i to the list of the corresponding key. 2. Using int Default FactoryWhen the int class is passed as the default_factory argument, then a defaultdict is created with default value as zero. Example: This example demonstrates using int as the default factory, making missing keys default to 0.
Output defaultdict(<class 'int'>, {1: 2, 2: 3, 3: 1, 4: 2})
Explanation: int() returns 0, so missing keys will have a default value of 0. The loop counts the occurrences of each number in the list a and updates the dictionary accordingly. 3. Using str Default FactoryWith defaultdict(str), any new key automatically maps to '', so you can concatenate text without key checks. Example: This example shows how str as a factory creates empty strings ("") for missing keys.
Output defaultdict(<class 'str'>, {'greeting': 'Hello'})
Explanation: str() returns an empty string, so missing keys will have an empty string as their default value. A value ('Hello') is explicitly set for the key 'greeting'. 4. Grouping Words by First Letterdefaultdict is very handy in text processing, for example grouping words by their starting letter. Example: This example demonstrates how defaultdict(list) can be used to group words by their first letter, very useful in text processing.
Output defaultdict(<class 'list'>, {'a': ['apple', 'ant'], 'b': ['banana', 'bat'], 'c': ['carrot', 'cat']})
Explanation: Here, defaultdict(list) automatically creates an empty list for each new first letter, so we can group words without checking if the key exists. Python defaultdict Type for Handling Missing KeysBehind the scenes, defaultdict uses the special __missing__() method:
Example: This example shows how the __missing__() method works behind the scenes in defaultdict. It is automatically called when a key is not found, returning the default value instead of raising a KeyError.
Output Not Present Not Present 1 Explanation:
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |