creating dictionnaries

Jim Meier fatjim at home.com
Tue Jun 22 16:41:26 EDT 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.

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


-Jim.





More information about the Python-list mailing list