Given the following dict,
combination_dict = {"one": [1, 2, 3], "two": [2, 3, 4], "three": [3, 4, 5]}
how would I achieve the following list?
result_list = [{"one": [1, 2, 3], "two": [2, 3, 4]}, {"one": [1, 2, 3], "three": [3, 4, 5]}, {"two": [2, 3, 4], "three": [3, 4, 5]}]
In other words, I want all combinations of two key/value pairs in a dict without replacement, irrespective of order.
One solution is to use itertools.combinations()
:
result_list = map(dict, itertools.combinations(
combination_dict.iteritems(), 2))
Due to popular demand, here a Python 3.x version:
result_list = list(map(dict, itertools.combinations(
combination_dict.items(), 2)))