How to perform an assignment inside a lambda function
Question: Python 2.7.1

I would like to understand why I can't do the following which seems like a
sensible thing to do

    
    
    def do_stuff():
        # return a function which takes a map as an argument and puts a key in there
        f = lambda map: map['x'] = 'y' #compilation error
        return f 
    
    x = do_stuff()
    map = {}
    x(map)
    print map['x']
    

I can have that lambda function to be somethign simpler something like `f =
lambda map: os.path.exists` however I cannot get it to change the map. Can
someone tell me how I can achieve this? If this is not possible at all why?


Answer: You can't use assignment in an expression, it is a statement. A `lambda` can
only contain _one_ expression, and statements are not included.

You _can_ assign to the map though, by using the [`operator.setitem()`
function](http://docs.python.org/2/library/operator.html#operator.setitem)
instead:

    
    
    import operator
    
    lambda map: operator.setitem(map, 'x', 'y')
    

