Zaz
Zaz

Reputation: 48769

Python: Sum string lengths

Is there a more idiomatic way to sum string lengths in Python than by using a loop?

length = 0
for string in strings:
    length += len(string)

I tried sum(), but it only works for integers:

>>> sum('abc', 'de')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sum() can't sum strings [use ''.join(seq) instead]

Upvotes: 20

Views: 30774

Answers (8)

gnikit
gnikit

Reputation: 1291

TLDR

If you care about performance use

len(''.join(strings))

else using map will suffice without sacrificing code readability or a lot of performance

sum(map(len, strings))

Performance metrics

Although I agree with the general consensus that when using Python your first priority should not be writing efficient and scalable code, I think it would be beneficial for this post to have some timings for the proposed answers.

Using the words from the first paragraph of lorem ipsum (list of strings excluded for the sake of brevity)

In [3]: timeit("""
    ...: length = 0
    ...: for s in strings:
    ...:     length += len(s)
    ...: """, globals=globals())
Out[3]: 5.197531974001322

In [4]: timeit("sum(len(s) for s in strings)", globals=globals())
Out[4]: 4.925184353021905

In [5]: timeit("sum(map(len, strings))", globals=globals())
Out[5]: 1.9876644779578783

In [6]: timeit("len(''.join(strings))", globals=globals())
Out[6]: 0.6793132669990882

So for large list of strings @Auspex is clearly to be prefered.

Upvotes: 2

Raymond Hettinger
Raymond Hettinger

Reputation: 226336

The shortest and fastest way is apply a functional programming style with map() and sum():

>>> data = ['a', 'bc', 'def', 'ghij']
>>> sum(map(len, data))
10

In Python 2, use itertools.imap instead of map for better memory performance:

>>> from itertools import imap
>>> data = ['a', 'bc', 'def', 'ghij']
>>> sum(imap(len, data))
10

Upvotes: 7

Auspex
Auspex

Reputation: 2254

I know this is an old question, but I can't help noting that the Python error message tells you how to do this:

TypeError: sum() can't sum strings [use ''.join(seq) instead]

So:

>>> strings = ['abc', 'de']
>>> print len(''.join(strings))
5

Upvotes: 5

shadab.tughlaq
shadab.tughlaq

Reputation: 454

Just to add upon ...

Adding numbers from a list stored as a string

nos = ['1','14','34']

length = sum(int(s) for s in nos)

Upvotes: -1

sk8asd123
sk8asd123

Reputation: 1705

Here's another way using operator. Not sure if this is easier to read than the accepted answer.

import operator

length = reduce(operator.add, map(len, strings))

print length

Upvotes: 1

Daenyth
Daenyth

Reputation: 37441

My first way to do it would be sum(map(len, strings)). Another way is to use a list comprehension or generator expression as the other answers have posted.

Upvotes: 18

Tony Veijalainen
Tony Veijalainen

Reputation: 5555

print(sum(len(mystr) for mystr in strings))

Upvotes: 2

liori
liori

Reputation: 42277

length = sum(len(s) for s in strings)

Upvotes: 49

Related Questions