Is there a nicer way to do this?

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Oct 5 03:31:15 EDT 2007


Stefan Arentz a écrit :
> Is there a better way to do the following?
> 
> 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.

There is:

attributeNames = dict(
     ("AttributeName.%d" % n, attr) \
     for n, attr in enumerate(attributes)
   )




More information about the Python-list mailing list