Alternative to dict comprehension prior to Python 2.7

Use:

gw_func_dict = dict((chr(2**i), func) for i, func in enumerate(gwfuncs[:8]))

That’s the dict() function with a generator expression producing (key, value) pairs.

Or, to put it generically, a dict comprehension of the form:

{key_expr: value_expr for targets in iterable <additional loops or if expressions>}

can always be made compatible with Python < 2.7 by using:

dict((key_expr, value_expr) for targets in iterable <additional loops or if expressions>)

Leave a Comment