Why does this raise an error?
I'm trying to add a value to the [0] index for the list

mylist=[]
mylist[0]='hello'

Recommended Answers

[icode]mylist[i][/icode] will raise IndexError unless i is in the interval [-n, n[ where n is the length of mylist. When the list is empty, this interval is empty. You could use
[code=python]
mylist.append("hello")
mylist.insert(0, "hello")
[/code]

Jump to Post

All 2 Replies

mylist[i] will raise IndexError unless i is in the interval [-n, n[ where n is the length of mylist. When the list is empty, this interval is empty. You could use

mylist.append("hello")
mylist.insert(0, "hello")