String formatting with two dictionaries?

Christian Tanzer tanzer at swing.co.at
Tue Oct 9 10:48:07 EDT 2001


> Thanks for all the answers. Seems as if I hadn't been specific
> enough about what I was looking for. It would be nice if there was
> some builtin dictionary magic that would allow
> 
> "%(foo) %(bar)" % magic(dict1, dict2, ...)
> 
> where in case of duplicate keys the leftmost dictionary would be
> preferred.
> 
> Perhaps the builtin dictionary() class could be extended so that
> it accepts more than one dictionary argument?

Really no need for a builtin here:

def magic(* dicts) :
    result = {}
    dicts  = list(dicts)
    dicts.reverse()
    map(result.update, dicts)
    return result

Adding keyword arguments is left as an exercise...

    

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list