1. 程式人生 > >python IndexError: list assignment index out of range

python IndexError: list assignment index out of range

在進行list的操作時報

IndexError: list assignment index out of range

故障解釋:索引錯誤:列表的索引分配超出列範圍

原始碼:
list=[]
list[0]='a'
輸出:

IndexError: list assignment index out of range


該error原因是定義的list是個空列表,list[0]本身不存在所以不能呼叫,可以使用append新增元素
list.append('a')
輸出:
['a']