Assigning a value to list by index out of range

list, python

Solution

In python single assignment does not work if the index does not exist. Javascript arrays are "sparse", you can stash a value at any index. Instead, python lists have a defined size, and assignment is only allowed in existing indices.

If you want to add at the end, use `mylist.append(value)`,

Problem

``` mylist = ["a", "apple", "b", "ball", "c", "cat"] mylist[6] = "value" print(mylist) ``` Error: ``` IndexError: list assignment index out of range ``` I remember assigning values by index in javascript, it doesn't work in python?

Original source

Related problems