Python: Call function that returns two strings inside map function

Question:

Hello I am trying to create a function that is called inside a map function, splits the string that have been passed as input and returns two processed strings. To be more understood here is my code (it doesn’t seem to return anything).

def prepare_data(data):
    x1, x2 = data.split(" ", 1) # split only 1 time at the space
    return x1.strip("""), x2 



if __name__ == "__main__":
  print(list(map(prepare_data, '"word_1" rest of sentence')))

Any suggestions would be appreciated. Cheers!

Asked By: C96

||

Answers:

Hi I believe the split function returns a list which cannot be unpacked into two variables. So its better to unpack it into a variable and return the elements in the list

Answered By: Irshad Ahamed

You need to make an iterable List from your sentence. Use:

x = map(prepare_data, ['"word_1" rest of sentence'])

print(list(x))
Answered By: user19077881
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.