Reputation:
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
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