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!
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
You need to make an iterable List from your sentence. Use:
x = map(prepare_data, ['"word_1" rest of sentence'])
print(list(x))
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!
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
You need to make an iterable List from your sentence. Use:
x = map(prepare_data, ['"word_1" rest of sentence'])
print(list(x))