PDA

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


ceql
11-24-2007, 12: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-24-2007, 01: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-24-2007, 01:39 PM
ahh Cheers Vishang! :) :beer:

vishangshah
11-27-2007, 07: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-27-2007, 07: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.