I used itertools and for loop as well, but I'm not sure what would be the pattern with more than 2 keys, as with itertools, for 3 keys, I'm getting one list element from each corresponding key,
d = {‘1’: ['a','b'], '2':['c','d'], '3' : ['m']}
output : acm adm bcm bdm
or should the output be: ac ad am bc bd bm cm dm
Q. Write a Python program to create and display all combinations of letters, selecting each letter from a different key in a dictionary.
Sample data: {‘1’: ['a','b'], '2':['c','d']}
expected Output:
ac
ad
bc
bd
My Code:-
dic = { '1' : ['a', 'b', '8'], '2' : ['c', 'd', 'm']}
i = 0;
key1 = list(dic.keys())[0]
key2 = list(dic.keys())[1]
for i in range(len(dic[key1])):
for j in range(len(dic[key2])):
print(dic[key1][i], end = "")
print(dic[key2][j])
output:-
ac ad am bc bd bm 8c 8d 8m