The Python documentation for sum shows that an iterable has to be passed in as the first argument for sum.
I was solving this leetcode problem 1295. Find Numbers with Even Number of Digits :
Given an array nums of integers, return how many of them contain an even number of digits.
I was solving this the long way like this:
# # longer as in more lines of code
class Solution:
def findNumbers(self, nums: List[int]) -> int:
count = 0
for num in nums:
if len(str(num)) % 2 == 0:
count += 1
return count
but decided to look at the other suggested answers to see if I missed something. One never knows with Easy questions on Leetcode. That's when I ran into this line of code that made me wonder if I understood what was going on.
from typing import List
# one line solution
class Solution:
def findNumbers(self, nums: List[int]) -> int:
return sum(len(str(num)) %2 == 0 for num in nums)
Could someone clarify how "future" booleans lead to an iterable? Is a list being created from the for loop?