你不能分配到像lst [i] = something这样的列表。你需要使用append。 lst.append(i)。

(如果使用字典,您可以使用赋值符号)。

创建空列表:

>>> l = [None] * 10
>>> l
[None, None, None, None, None, None, None, None, None, None]
range(x)从[0,1,2,… x-1]创建一个列表,
# 2.X only. Use list(range(10)) in 3.X.
>>> l = range(10)
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

使用函数创建列表:

>>> def display():
... s1 = []
... for i in range(9): # This is just to tell you how to create a list.
... s1.append(i)
... return s1
...
>>> print display()
[0, 1, 2, 3, 4, 5, 6, 7, 8]

列表推导(使用方块,因为对于范围你不需要做所有这些,你可以只返回range(0,9)):

>>> def display():
... return [x**2 for x in range(9)]
...
>>> print display()
[0, 1, 4, 9, 16, 25, 36, 49, 64]