Skip to content
Advertisement

Python: reduce (list of strings) -> string

JavaScript

I expected something like ‘a.b.c.d’ or ‘abcd’. Somehow, can’t explain the results. There are similar questions here, but not quite like this one.

Advertisement

Answer

The result of executing the function passed as the first parameter, will be the first parameter to that function in the next iteration. So, your code works like this

JavaScript
  1. When x, y are 'alfa' and 'bravo' respectively, a.b.

  2. Now, x will be a.b and y will be 'charlie', so result will be a.c

  3. Now, x will be a.c and y will be 'delta', so result will be a.d

That is why the result is a.d

To get what you wanted, take all the first characters from all the strings to form a list and join all the elements together with ., like this

JavaScript

Note: I won’t prefer this way, but for the sake of completeness, you can do it with reduce, like this

JavaScript

Now, the last empty string will be the first value for x in the first iteration. And we use . only if x is not an empty string otherwise we use an empty string, so that the concatenation would give the result you wanted.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement