Python Reduce List of Strings to String

Viewed 298

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 Answers

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) 
Related