Alternative to dict comprehension prior to Python 2.7
10,521
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>)
Author by
stdcerr
Coding mostly on embedded systems (24/7/365 uptime) to make a living at daytime, coding on a variety of systems (x86, arm) for fun at nighttime, preferences: Linux, C & C++. Always interested in learning other ways/better ways to solve problems!
Updated on June 03, 2022Comments
-
stdcerr almost 2 years
How can I make the following functionality compatible with versions of Python earlier than Python 2.7?
gwfuncs = [reboot, flush_macs, flush_cache, new_gw, revert_gw, send_log] gw_func_dict = {chr(2**i): func for i, func in enumerate(gwfuncs[:8])}