a clean way to define dictionary

Alex Martelli aleax at aleax.it
Wed Jun 18 04:18:31 EDT 2003


Sean 'Shaleh' Perry wrote:

> On Tuesday 17 June 2003 21:35, Kendear wrote:
>>
>> or is there a more common way to define a dictionary
>> without all the punctuation marks?
> 
>>>> def make_dict(**kwargs):
> ...     return kwargs
> ...
>>>> make_dict(foo=1, bar='sean')
> {'foo': 1, 'bar': 'sean'}
> 
> is the most common form I have seen.  Not sure if it works for you.

In 2.3, you can express this as dict(foo=1, bar='sean') without a
need to define a function for the purpose.  The constraints are the
same, i.e., all keys must be strings, each syntactically a valid
Python identifier (and not a keyword).


Alex





More information about the Python-list mailing list