I
I
irina_leifijtijhiodu2021-07-11 12:06:52
Python
irina_leifijtijhiodu, 2021-07-11 12:06:52

Adding list elements via sum in python doesn't work, why?

Good afternoon!

Code:
list = [1, 2, 3, 4]
sum(list)

Throws an error:
TypeError Traceback (most recent call last)
in ()
12
13 list = [1, 2, 3, 4]
---> 14 sum (list)

TypeError: 'int' object is not callable

What could be the problem, like sum is applied to lists or not?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Nevzorov, 2021-07-11
@irina_leifijtijhiodu

Somewhere above you overridden the value of the "sum" built-in function to a number ( sum=0).
Do not use the names of built-in functions for your own variables (sum, list).

P
Pavel Shvedov, 2021-07-11
@mmmaaak

most likely somewhere earlier in the code the sum variable, which contains int, is redefined, the code is working

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question