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

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")

ah.. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.