Is there a nicer way to do this?

Tim Chase python.list at tim.thechases.com
Thu Oct 4 23:38:50 CEST 2007


> attributes = ['foo', 'bar']
> 
> attributeNames = {}
> n = 1
> for attribute in attributes:
>    attributeNames["AttributeName.%d" % n] = attribute
>    n = n + 1
> 
> It works, but I am wondering if there is a more pythonic way to
> do this.

"Better" may be subjective, but you could do something like

   attributes = ['foo', 'bar']
   attributeNames = dict(
       ("AttributeName.%d" % (i+1), attrib)
       for i, attrib
       in enumerate(attributes)
     )

HTH,

-tkc








More information about the Python-list mailing list