def dictize(aniter, mode, initial=None):
    """iter must contain (key,value) pairs. mode is a string, one of: replace, keep,
    tally, sum, append, or a custom function that takes two arguments. 
    
    replace: default dict behavior. New value overwrites old if key exists. This
    is essentially a pass-thru.
    keep: Noop if kv already exists in dict.
    tally: Ignore value, count how many times each key occurs.
    sum: Each key contains a sum of the (presumably summable) values that arrive
    with that key.
    append: each key contains a list of the items that arrived with that key.
    add: each key contains a set of the items that arrived with that key.
    Custom func: The first argument is the existing key value. This function
    won't be called if the key doesn't exist. The second is the newly arrived value. 
    The return value will replace the existing value in the internal dict

    initial optional argument: function that gets called the first time a key
    occurs. It's parameter is the value. It's return is placed in the dict. Use
    to specify a default value."""
    data = {}
    modes = "replace keep tally sum append add".split(' ')
    funcs = [lambda e, n: n,
             lambda e, n: e,
             lambda e, n=None: e+1,
             lambda e, n: e+n,
             lambda e, n: e+[n],
             lambda e, n: e.union([n])]
    inits = [lambda v: v,
             lambda v: v,
             lambda v: 1,
             lambda v: v,
             lambda v: [v],
             lambda v: set([v])]
    if mode in modes:
        modei = modes.index(mode)
        func = funcs[modei]
        if not initial:
            initial = inits[modei]
    else:
        assert hasattr(mode, '__call__'), '2nd argument must be a function or\
        one of: %s' % ' '.join(modes)
        func = mode
        if not initial:
            initial = lambda x: x
        
    for (k, v) in aniter:
        if k in data:
            data[k] = func(data[k], v)
        else:
            data[k] = initial(v)
    return data