Search code examples
pythonlambdadefaultdict

Using lambda and defaultdict


I was reading about the collection defaultdict and came across these lines of code:

import collections
tree = lambda: collections.defaultdict(tree)
some_dict = tree()
some_dict['colours']['favourite'] = "yellow"

I understand that lamba takes a variable and performs some function on it. I've seen lambda being used like this: lambda x: x + 3 In the second line of code above, what variable is lambda taking and what function is it carrying out?

I also understand that defaultdict can take parameters such as int or list. In the second line, defaultdict takes the parameter tree which is a variable. What is the significance of that?


Solution

  • The code is roughly equivalent (ignoring metadata introduced by the def statement) to

    import collections
    def tree():
        return collections.defaultdict(tree)
    some_dict = tree()
    some_dict['colours']['favourite'] = "yellow"
    

    The lambda expression simply defines a function of zero parameters, and the function is bound to the name tree.

    Typically, you only use lambda expressions when you actually want an anonymous function, for example passing it as an argument to a another function, as in

    sorted_list = sorted(some_list_of_tuples, key=lambda x: x[0])
    

    It is considered better practice to use a def statement when you really want a named function.


    defaultdict takes a callable to be used to produce a default value for a new key. int() returns 0, list() returns an empty list, and tree() returns a new defaultdict; all of them can be used as arguments to defaultdict. The recursive nature of defining tree to return a defaultdict using itself as the default-value generator means you can generate nested dicts to an arbitrary depth; each "leaf" dict is itself another defaultdict.