I kept running into the same need and ended up making the following function:
def kwarg_map(element_constructor, **kwarg_lists):
"""
A helper function for when you want to construct a chain of objects with individual arguments for each one. Can
be easier to read than a list expansion.
:param element_constructor: A function of the form object = fcn(**kwargs)
:param kwarg_lists: A dict of lists, where the index identifies two which element its corresponding value will go.
:return: A list of objects.
e.g. Initializing a chain of layers:
layer_sizes = [784, 240, 240, 10]
layers = kwarg_map(
Layer,
n_in = layer_sizes[:-1],
n_out = layer_sizes[1:],
activation = ['tanh', 'tanh', 'softmax'],
)
is equivalent to:
layers = [Layer(n_in=784, n_out=240, activation='tanh'), Layer(n_in=240, n_out=240, activation='tanh'), Layer(n_in=240, n_out=10, activation='softmax')]
"""
all_lens = [len(v) for v in kwarg_lists.values()]
assert len(kwarg_lists)>0, "You need to specify at least list of arguments (otherwise you don't need this function)"
n_elements = all_lens[0]
assert all(n_elements == le for le in all_lens), 'Inconsistent lengths: %s' % (all_lens, )
return [element_constructor(**{k: v[i] for k, v in kwarg_lists.iteritems()}) for i in xrange(n_elements)]