out of range"?">

List Assignment Index Out Of Range Error Python

CGTalk > Autodesk > Autodesk Maya > Maya Programming > quickie python question: "list assignment index out of range"


PDA

View Full Version : quickie python question: "list assignment index out of range"


ceql

11 November 2007, 01:53 PM

I've just started learning python, and am porting some old mel scripts over to python, but I've run across a seemingly simple snag!

I want to declare an empty list, and then later on put a value into a specific index...


myArray = []
myArray[0] = "test"


it errors:


# Error: list assignment index out of range
# Traceback (most recent call last):
# File "<maya console>", line 2, in <module>
# IndexError: list assignment index out of range #


It's like the list length is locked - am I missing something obvious here? :D

Any help would be appreciated!


vishangshah

11 November 2007, 02:34 PM

when You initialize any array in python with

myarray = []

it declared the array with zero array members, so first you have to add some array elements,

use like this, for adding elements,

myarray = []
myarray.append("someValue")

now u can access element myarray[0]

myarray[0] = "newValue"

:)
Vishang Shah


ceql

11 November 2007, 02:39 PM

ahh Cheers Vishang! :) :beer:


vishangshah

11 November 2007, 08:21 AM

Hi,

I have started a blog on python development for CG,

you can check out here,

http://shahvishang.blogspot.com/



Happy Developing
:)

Vishang Shah


CGTalk Moderation

11 November 2007, 08:21 AM

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.



[Tutor] list initialization question

amoreira@mercury.ubi.pt amoreira@mercury.ubi.pt
Mon, 03 Apr 2000 14:55:19 +0000
Hello! You can't assign to an element of a list before you create it. Then, either you populate it before (for instance, with zeros: x=[0]*11), or you use the append method of lists: x=[] for i in range(11): x.append(whrandom.randint(1,100)) print i,x[i] In this case, I'd choose the x.append way. You can add elements to a list using the append method, not by straight away assignement. Hope it helps, Ze Amoreira amoreira@mercury.ubi.pt Tim Condit wrote: > Why does this fail... >> >>> x = [] > >>> for i in range(11): > ... x[i] = whrandom.randint(1, 100) > ... print i, x[i] > ... > Traceback (innermost last): > File "<stdin>", line 2, in ? > IndexError: list assignment index out of range >> ... but this succeeds? >> >>> x = range(11) > >>> x > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > >>> for i in range(11): > ... x[i] = whrandom.randint(1, 100) > ... print i, x[i] > ... > Is it necessary to not only initialize a list, but also to populate it? Or > am I doing something wrong (I hope so..)? If you can add, delete, etc. > items to a list, then why am I getting the error "list assignment index > out of range"? >> Thanks! > Tim
Categories: 1

0 Replies to “List Assignment Index Out Of Range Error Python”

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *