Can someone explain the structure of reduce() in the following example:
def f2(list):
return reduce(lambda string, item: string + chr(item), list, "")
I know that f2 converts a list of int’s into a string, but my problem is understanding
reduce in this context.
I know the basic structure of reduce is reduce(function, sequence[, initial]) but this
is somehow confusing to me.
Can someone explain reduce(lambda string, item: string + chr(item), list, “”) and give me some similar examples ?
Thanks in advance.
The code applies
chr()to every element of the list, and concatenates the results into a single string.The
reduce()call is equivalent to the following:The
""is the third argument toreduce(). The lambda function inis called for every item in the list. It simply appends
chr(item)to the result of the previous iteration.For more examples of using
reduce(), see Useful code which uses reduce() in python