I am trying to append objects to the end of a list repeatedly, like so:
list1 = []
n = 3
for i in range(0, n):
list1 = list1.append([i])
But I get an error like: AttributeError: 'NoneType' object has no attribute 'append'. Is this because list1 starts off as an empty list? How do I fix this error?
appendactually changes the list. Also, it takes an item, not a list. Hence, all you need is(By the way, note that you can use
range(n), in this case.)I assume your actual use is more complicated, but you may be able to use a list comprehension, which is more pythonic for this:
Or, in this case, in Python 2.x
range(n)in fact creates the list that you want already, although in Python 3.x, you needlist(range(n)).