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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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),