Python defaultdict with string as type of value
Question: Need to create a defaultdict, value type is normal Python string, it seems my
below method does not work? Post compile error message. Using Python 2.7 and
wondering any good ideas how to fix? Thanks.

**Code**

    
    
    import collections
    
    a = collections.defaultdict("")
    
    a[1]="Hello"
    a[2]="World"
    
    print a
    

**Error Message**

    
    
      a = collections.defaultdict("")
    TypeError: first argument must be callable
    


Answer: As the error says, the first argument has to be a callable that produces the
value you want. Use `str`:

    
    
    a = collections.defaultdict(str)
    

If necessary, you can create a wrapper with a `lambda` function:

    
    
    a = collections.defaultdict(lambda: 'initial')
    

