pythonpython-3.xlistdefaultdict

Python defaultdict(list) behavior


I was playing around with Chatgpt and it kind of suprised me that if you declare

node = defaultdict(list)
node['xyz'] = 'xyz'

adding a new string key-value pair. I thought node will create a new list when 'xyz' key doesn't present in the node, therefore if you assign A string to [], it will probably return a runtime error or something. But this one actually works, according to gpt. Any reason why this works? are there any docs I can read up about this?


Solution

  • collections.defaultdict will automatically return an empty list (assuming list is the provided default_factory) if you access a missing key, but will behave like a normal dictionary otherwise.

    From the documentation:

    Return a new dictionary-like object. defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the dict class and is not documented here.

    The first argument provides the initial value for the default_factory attribute; it defaults to None. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.

    defaultdict objects support the following method in addition to the standard dict operations:

    __missing__(key)

    If the default_factory attribute is None, this raises a KeyError exception with the key as argument.

    If default_factory is not None, it is called without arguments to provide a default value for the given key, this value is inserted in the dictionary for the key, and returned.

    If calling default_factory raises an exception this exception is propagated unchanged.

    This method is called by the __getitem__() method of the dict class when the requested key is not found; whatever it returns or raises is then returned or raised by __getitem__().

    Note that __missing__() is not called for any operations besides __getitem__(). This means that get() will, like normal dictionaries, return None as a default rather than using default_factory.