You can solve the problem using Python's itertools.product. Here's the way I solved it.
from itertools import product
def solution(words, syn):
new_words_list = []
for w in words:
if w in syn.keys():
new_words_list.append(syn[w] + [w])
else:
new_words_list.append([w])
answer = list(product(*new_words_list))
return answer
Given that your words is given as a list of strings and syn is given as a dictionary where the key is a word in the words and value is the list of synonym(s), take words and syn as inputs and generate a nested list called new_words_list of every possible case for each word.
[['feline', 'cat'], ['below', 'down', 'beneath', 'under'], ['bench', 'board', 'table']]
Since the length of words and syn are variables, use a * of list comprehension operation to pass the nested list, new_words_list, to itertools.product(). itertools.product() computes the Cartesian product of given iterables.
The output of this code snippet would be as follows.
['feline', 'below', 'bench']
['feline', 'below', 'board']
['feline', 'below', 'table']
['feline', 'down', 'bench']
['feline', 'down', 'board']
['feline', 'down', 'table']
['feline', 'beneath', 'bench']
['feline', 'beneath', 'board']
['feline', 'beneath', 'table']
['feline', 'under', 'bench']
['feline', 'under', 'board']
['feline', 'under', 'table']
['cat', 'below', 'bench']
['cat', 'below', 'board']
['cat', 'below', 'table']
['cat', 'down', 'bench']
['cat', 'down', 'board']
['cat', 'down', 'table']
['cat', 'beneath', 'bench']
['cat', 'beneath', 'board']
['cat', 'beneath', 'table']
['cat', 'under', 'bench']
['cat', 'under', 'board']
['cat', 'under', 'table']