Skip to content

Instantly share code, notes, and snippets.

from itertools import product
def combinations(d):
"""
>>> d = {'a': [2, 5, 6], 'b': [8, 3, 6]}
>>> combinations(d)
[{'a': 2, 'b': 8}, {'a': 2, 'b': 3}, {'a': 2, 'b': 6}, {'a': 5, 'b': 8}, {'a': 5, 'b': 3}, {'a': 5, 'b': 6}, {'a': 6, 'b': 8}, {'a': 6, 'b': 3, {'a': 6, 'b': 6}]
"""
return [dict(combo) for combo in product(*[
@lssergey
lssergey / flatten.py
Created November 8, 2012 20:44
List flattening
# Python does not have tail recursion optimization,
# so this function can't handle too deep data structures.
# This piece of code was written as a practicing task.
def flatten(l):
nested = None
for i, e in enumerate(l):
if isinstance(e, list):
nested = e; break
if nested is not None: