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

前端 未结 2 1595
生来不讨喜
生来不讨喜 2021-01-18 07:47

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          


        
相关标签:
2条回答
  • 2021-01-18 07:48

    l.append[i]

    Wrong parenthesis.

    0 讨论(0)
  • 2021-01-18 07:50

    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]
    
    0 讨论(0)
提交回复
热议问题