append actually modifies the list. In addition, an item is required, not a list. Therefore, all you need is
for i in range(n): list1.append(i)
(By the way, note that in this case you can use range(n) .)
I assume your actual use is more complex, but you can use a list comprehension, which is more python for this:
list1 = [i for i in range(n)]
Or, in this case, in Python 2.x range(n) actually creates the list you want already, although in Python 3.x you need list(range(n)) .
Andrew Jaffe Jun 14 '11 at 5:01 2011-06-14 05:01
source share