2

I am currently working on a problem where I need to reduce a list of strings into a single string, modifying each string slightly. For example, given input ["apple", "pear", peach"], I want "apple0 pear0 peach0" as the output.

With the reduce function I am using:

reduce(lambda x,y: x + "0 " + y, string_list)

I am getting an output of "apple0 pear0 peach", without the modification on the last element in the input list. I want to resolve this so that my last element gets modified as well.

1 Answer 1

3

Consider l is your list with join

' '.join(map(lambda x : x+'0',l))
'apple0 pear0 peach0'

Or

'0 '.join(l)+'0'
'apple0 pear0 peach0'

Based on comment from @Bobby

' '.join(x+'0' for x in l) 
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need to define the lambda in memory and can just do ' '.join(x+'0' for x in l)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.