I'm trying to loop a list and unable to add it to the string in python

For example, I do have a code like below, where

res = get_prediction_eos(input_text)
print("res is : ", res)

answer = []

print("res ", res['bert'].split("\n"))

for i in res['bert'].split("\n"):
     answer.append(i)
answer_as_string = "    ".join(answer)

print("answer is : ", answer_as_string)

And the output for the above code is returned as below :

res is :  {'bert': 'up\nwrong\nhappening\nnew\nhappened'}
res  ['up', 'wrong', 'happening', 'new', 'happened']
answer is :  up    wrong    happening    new    happened

I do have a text like below code :

text = "Hey what's"

The main required output is ‘answer is : up wrong happening new happened’. I do have a text like ‘Hey what’s’ and i want to add the answer’s outputs to the text like

Hey what's up
Hey what's wrong
Hey what's happening
Hey what's new
Hey what's happened

Can anyone help me with the code?

>Solution :

You just need to iterate over the list and print text + the element in the loop.

In Python >= 3.6:

for e in res['bert'].split('\n'):
    print(f'{text} {e}?')

Leave a Reply