Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

l.append[i], object is not subscriptable? [closed]

When I do:

l = []
for i in range(10):
    if i%3 == 0 or i%5 == 0:
        l.append[i]
print sum(l)

I get

Traceback (most recent call last):
  File "PE1.py", line 4, in <module>
    l.append[i]
TypeError: 'builtin_function_or_method' object is not subscriptable

Is there really no way append all the i's that pass the condition?

like image 773
Bentley4 Avatar asked Apr 12 '12 21:04

Bentley4


2 Answers

append is a method, you use function call syntax.

l.append(i)

Also, more elegant approach in cases like this is to use list comprehension:

l = [i for i in range(10) if i % 3 == 0 or i % 5 == 0]
like image 90
Cat Plus Plus Avatar answered Nov 14 '22 17:11

Cat Plus Plus


l.append[i]

Wrong parenthesis. You should use:

l.append(i)

like image 23
rkhayrov Avatar answered Nov 14 '22 17:11

rkhayrov