user7074495
user7074495

Reputation:

Printing the combined length of items in list for python

I'm trying to find out the combined length of all of the items inside of a list.

list = [12, 35]

i'm not quite sure how to calculate the len of all the items inside in one go, so that it returns '4', instead 2, the amount of items in the list. Thanks!

Upvotes: 0

Views: 363

Answers (2)

Thmei Esi
Thmei Esi

Reputation: 442

Slightly more pythonic.

len(''.join(map(str, lst)))

Upvotes: 0

MSeifert
MSeifert

Reputation: 152657

I assume that the length of a number is given by the number of digits.

The easiest way would be to convert everything to a string and then add their lengths:

>>> lst = [12, 35]
>>> sum(len(str(item)) for item in lst)
4

Upvotes: 3

Related Questions