Is there a nicer way to do this?
Terry Reedy
tjreedy at udel.edu
Thu Oct 4 17:53:14 EDT 2007
"Stefan Arentz" <stefan.arentz at gmail.com> wrote in message
news:87ejgawpug.fsf at keizer.soze.com...
|
| 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.
Perhaps better is using enumerate:
>>> attributeNames = {}
>>> for n,attribute in enumerate(['foo', 'bar']):
attributeNames["AttributeName.%d" % (n+1)] = attribute
>>> attributeNames
{'AttributeName.1': 'foo', 'AttributeName.2': 'bar'}
However, mapping indexes to names should be more useful:
>>> aNames = dict(enumerate(['foo', 'bar']))
>>> aNames
{0: 'foo', 1: 'bar'}
Terry Jan Reedy
More information about the Python-list
mailing list