Skip to content

Instantly share code, notes, and snippets.

@minstrel271
Last active March 11, 2016 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minstrel271/d51654af3fa4e6411267 to your computer and use it in GitHub Desktop.
Save minstrel271/d51654af3fa4e6411267 to your computer and use it in GitHub Desktop.
from itertools import product
def named_product(**kwargs):
"""
Cartesian product of input iterables with keys.
Due to how python handle kwargs, the order of the keys is not conserved.
>>> dict(x=1, y='a') in list(named_product(x=[1, 2], y=['a', 'b']))
True
"""
yield from (dict(zip(kwargs.keys(), prod))
for prod in product(*kwargs.values()))
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment