Is there a nicer way to do this?

Casey Caseyweb at gmail.com
Thu Oct 4 23:42:55 CEST 2007


Not sure if this is really better or even more pythonic, but if you
like one-liners that exercise the language:

attributeNames = dict( [("AttributeName.%d" % (n+1), attribute) for
n,attribute in enumerate(attributes)] )

What this does is create a list (using a list comprehension and the
enumerate function) of ("AttributeName.x", attribute) tuples which is
then be used to initialize a dictionary.




More information about the Python-list mailing list