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?
collections.defaultdictwill automatically return an empty list (assuminglistis the provideddefault_factory) if you access a missing key, but will behave like a normal dictionary otherwise.From the documentation: