*** UPDATE **** The additional letter "A" was a problem. I'm rephrasing this here. Perhaps clearer? I have to replace values in a list using a dictionary which has lists of variable lengths as its values. For example:
variants = {C:["cat", "can", "car"], D:["do","die"], Z:["zen", "zoo"]}
And a list:
Letters = ["C", "D", "Z"]
I want a list output like this
PotentialWords = [["cat", "do", "zen"], ["can", "die", "zoo"],["car", "do", "zen"], ["car", "die", "zoo"]
where all the elements get updated at each step, but if the index exceeds, then the updates are preserved and we get all the variants crossed with each other.
What I have so far is:
max_len = max([len(words) for words in variants.values()])
for i in range(max_len):
var = []
for let in Letters:
if let not in variants.keys():
var.append(let)
else:
if i < len(variants[let]):
var.append(variants[let][i])
elif i > len(variants[let]):
var.append(let)
Which gives the erroneous output:
OutputWords = [["cat", "do", "zen"], ["can", "die", "zoo"], ["car"]]
All your kind help will be deeply appreciated :) * UPDATE* This question has been updated to make clearer, thanks to the commenters. Previous input was
Letters = ["C", "D", "Z", "A"]
And output
[["cat", "do", "zen", "A"], ["can", "die", "zoo","A"],["car", "A"]]
** please look at only the above input/outpt