0

I have a list of words and I am trying to compute the total length of all words combined. Why is my code:

words = ["a", "ab", 'adsf']                                             
    
wordlen = reduce(lambda x, y: len(x) + len(y), words) 

giving me:

Traceback (most recent call last)
  wordlen = reduce(lambda x, y: len(x) + len(y), words)
TypeError: object of type 'int' has no len()
3
  • Your function returns an int. From the second iteration onwards of reduce, the x argument gets the previous return value (which is an int...). That's just how reduce works Commented Sep 27, 2020 at 15:43
  • @Tomerikoo isnt that a strange implementation of reduce? It is a bit different from my experience with reduce in other languages. Commented Sep 27, 2020 at 15:45
  • No I don't think it is, and anyway it is well documented. It reduces the iterable to a single value by applying the function on first 2 elements, and then applying it on the result and the next element cumulatively. This implies that the arguments and return value need to have the same type Commented Sep 27, 2020 at 15:48

3 Answers 3

3

Below is a little different approach to the problem:

words = ['zz','abc']
total_len = sum(len(x) for x in words)
print(total_len)

output

5
Sign up to request clarification or add additional context in comments.

2 Comments

Or in a more functional fashion: print(sum(map(len, words)))
But the question is not how to do that, it is why the provided method doesn't work...
1

If you want to use reduce, use initializer parameter and remove first len():

from functools import reduce

words = ['Hello', 'World', 'This', 'Is', 'Example']

wordlen = reduce(lambda x, y: x + len(y), words, 0)

print(wordlen)

Prints:

23

1 Comment

@DeepSpace If length of words is 2 then it works, otherwise it fails.
0

Inspired by @Tomerikoo's comment, I did a deeper research on reduce (aka fold). Many of the reduce/fold implementations in other languages require the operation to be associative. When the operation is associative, the order it is applied does not matter. For example, len("a") + len("b") + len("c") generates same results wherever you put the parenthesis (because integer addition on a finite field is associative).

The confusion happens when the operation is not associative. In this case the order the function is applied matters. This is when the concept of left fold and right fold comes in. They are both ways that stipulate how parenthesis is added.

Also note that fold requires two functions: Function i that converts an object of type A to another of type B and Functoin ii that reduces multiple type B objects into one single object of type C.

Left fold and right fold essentially use pairwise operations, which allows one to combine the two functions into one. This is the idea of @Andrej Kesely's answer.

Comments

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.