I have 2 lists:
list1 = ['A', 'B', 'C']
list2 = ['1', '2', '3']
And I want to create a dictionary with all possible combinations of list2 with list1, something like:
output= [{'A':'1', 'B':'1', 'C':'1'},{'A':'2', 'B':'1', 'C':1'} ..., {'A':'3', 'B':'3', 'C':'3'}]
I tried:
combinations = ([dict(zip(list1,v)) for v in product(list2)])
But isn't what I expected
import itertools
list1 = ['A', 'B', 'C']
list2 = ['1', '2', '3']
output = []
for p in itertools.product(list2, repeat=len(list1)): # (1,1,1),(1,1,2),...,(3,3,3)
# print (dict(zip(list1, p)))
output.append(dict(zip(list1, p)))
print (output)
# One line
output = [dict(zip(list1, p)) for p in itertools.product(list2, repeat=len(list1))]
itertools.product
returns all possible value-"pairs" you want, with the argument repeat
denoting the length of each permutation(with repetition?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With