Why does my code produce error "'int' object is not iterable"
Question:
Here is the leetcode question: [Richest customer wealth][1]
and here is my solution:
class Solution:
def maximumWealth(self, accounts: list[list[int]]) -> int:
res = []
for wealth in accounts:
res = (sum(wealth))
return max(res)
and I got the error such as:
TypeError: 'int' object is not iterable
return max(res)
Line 6 in maximumWealth (Solution.py)
ret = Solution().maximumWealth(param_1)
Line 25 in _driver (Solution.py)
_driver()
Line 36 in <module> (Solution.py)
I simply converted this code into code above but I don’t know why this works and the another doesn’t?
class Solution:
def maximumWealth(self, accounts: list[list[int]]) -> int:
# 리스트 내포 사용
accounts = [sum(wealth) for wealth in accounts]
return max(accounts)
Answers:
You want to add your wealth summary to a list and not replace it. So instead of
res = []
for wealth in accounts:
res = (sum(wealth))
you want last line to look like
res.append(sum(wealth))
Your problem is that you haven’t ported the code correctly. Your line res = (sum(wealth))
is equivalent to just saying res = sum(wealth)
. As sum outputs a number, res just stores this value. Then, your code tries to take the max of a single number, and fails:
>> max(1)
TypeError: 'int' object is not iterable
The fix posted by Marcin is correct, but there’s no need to get rid of the list comprehension, really.
That is because sum(wealth) returns an int and you replace the list res with it each time in the loop with:
res = (sum(wealth))
and then at the last line in the method you do max(res) on the last saved int which is not allowed for integers but is allowed for lists since max require an iterable type.
Here is the leetcode question: [Richest customer wealth][1]
and here is my solution:
class Solution:
def maximumWealth(self, accounts: list[list[int]]) -> int:
res = []
for wealth in accounts:
res = (sum(wealth))
return max(res)
and I got the error such as:
TypeError: 'int' object is not iterable
return max(res)
Line 6 in maximumWealth (Solution.py)
ret = Solution().maximumWealth(param_1)
Line 25 in _driver (Solution.py)
_driver()
Line 36 in <module> (Solution.py)
I simply converted this code into code above but I don’t know why this works and the another doesn’t?
class Solution:
def maximumWealth(self, accounts: list[list[int]]) -> int:
# 리스트 내포 사용
accounts = [sum(wealth) for wealth in accounts]
return max(accounts)
You want to add your wealth summary to a list and not replace it. So instead of
res = []
for wealth in accounts:
res = (sum(wealth))
you want last line to look like
res.append(sum(wealth))
Your problem is that you haven’t ported the code correctly. Your line res = (sum(wealth))
is equivalent to just saying res = sum(wealth)
. As sum outputs a number, res just stores this value. Then, your code tries to take the max of a single number, and fails:
>> max(1)
TypeError: 'int' object is not iterable
The fix posted by Marcin is correct, but there’s no need to get rid of the list comprehension, really.
That is because sum(wealth) returns an int and you replace the list res with it each time in the loop with:
res = (sum(wealth))
and then at the last line in the method you do max(res) on the last saved int which is not allowed for integers but is allowed for lists since max require an iterable type.