creating dictionnaries

Jim Meier fatjim at
Tue Jun 22 22:41:26 CEST 1999

Christian Caremoli wrote:

> Hi,
> Dictionnaries can be created like that :
> d={'a':1,'b':2}
> By calling a function you create a dictionnary like that :
> def f(**d):
>    return d
> d=f(a=1,b=2)
> I would like to be able to create dictionnaries with some similar syntax
> like keyed tuples :
> d=(a=1,b=2)
> Is there a way to do that ?

You betcha. Just build a function like this one:

def dict(**kwargs):
    return kwargs

and shazam! You've got your keyword args style dictionary creator.

>>> dict(a=5, b='jim')
{'b':'jim', 'a':5}


More information about the Python-list mailing list