• Home   /  
  • Archive by category "1"

Index Error List Assignment Index Out Of Range 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.



I recently started to use python and i am still newbie with many things of the language. This piece of code should print a serie rows (Ex.:[47.815, 47.54, 48.065, 57.45]) that i take as imput from several text files (called 2t1,...,2t19,3t1,...,3t19) and the name of the file after it's over. Every file has 80 elements that must be saved (for a total of ~12000 elements, ~80 rows for each file, 4 elements each row)

It is able to do what i ask it, but just until 2t5 and then it is shown this message:

Traceback (most recent call last):

File "< tmp 1>", line 8, in < module>

row[i][j] = [float(item) for item in line.split()]

IndexError: list assignment index out of range

I can't really figure out what's the problem, i guess the list isn't filled to it's max (i read online that it has 500.000.000 spaces), and i can't see what may be wrong . That error appears when the list you are working on is empty, but i doublechecked and there is every file, but i also tryed with (that is the solution for the most common mistakes) and it returns me a really long list of this:

< generator object < genexpr> at 0x000000000XXXXXXX>

(Where the X are different numbers) ending then with the same error.

P.s. If you need the data i'm working on just ask and i'll upload it.

pythonarrays

One thought on “Index Error List Assignment Index Out Of Range Python

Leave a comment

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