0
Python: How can I fix this type error?
zzz = [(5,8,0),(2,4)] sum = [] for x, *y in zzz: # What does the * means? Why do we need a comma? sum = sum+y print(sum(sum)) # outputs TypeError: 'list' object is not callable According to https://stackoverflow.com/questions/31087111/typeerror-list-object-is-not-callable-in-JUMP_LINK__&&__python__&&__JUMP_LINK/31087151, the code used a builtin name as a variable, and the fix is to rename the offending variable From my understanding, sum is the offending variable, and is initially redefined as an empty list. So what exactly is the problem here? This empty list is assigned itself plus y. What is y in this case? And how is *y involved?
1 Odpowiedź
+ 5
The problem is that you have called your variable "sum" the same name as a builtin method and then tried to call that method.
zzz = [(5,8,0),(2,4)]
sum_ = []
for x, *y in zzz: # What does the * means? Why do we need a comma?
sum_ = sum_+y
print(sum(sum_)) # outputs 12
x, *y = (5,8,0) # here, x takes the first value and y takes the rest as a list due to the "*"; x = 5, y = [8,0]