2

I'm trying to set the index of an array in Python, but it isn't acting as expected:

theThing = []
theThing[0] = 0
'''Set theThing[0] to 0'''

This produces the following error:

Traceback (most recent call last):
  File "prog.py", line 2, in <module>
    theThing[0] = 0;
IndexError: list assignment index out of range

What would be the correct syntax for setting an index of an array in Python?

6
  • Here it is on ideone, with the same error: ideone.com/0IV1Sc#view_edit_box Commented Apr 5, 2013 at 2:00
  • theThing = [] creates an empty array, so the index 0 doesn't exist. Commented Apr 5, 2013 at 2:01
  • 1
    I'm relatively unfamilar with Python (coming from a JavaScript background), so I found this to be surprising. In JavaScript, you can simply do var theThing = new Array(); theThing[0] = 0; to set the 0th element of theThing to 0. Commented Apr 5, 2013 at 2:01
  • instead of theThing[0]=0, try theThing.append(0). Commented Apr 5, 2013 at 2:06
  • It turns out that it's actually possible to initialize an array with a specific size in Python: stackoverflow.com/questions/6142689/… Commented Apr 5, 2013 at 2:47

3 Answers 3

5

Python lists don't have a fixed size . To set the 0th element, you need to have a 0th element:

>>> theThing = []
>>> theThing.append(12)
>>> theThing
[12]
>>> theThing[0] = 0
>>> theThing
[0]

JavaScript's array object works a bit differently than Python's, as it fills in previous values for you:

> x
[]
> x[3] = 5
5
> x
[undefined × 3, 5]
Sign up to request clarification or add additional context in comments.

6 Comments

How does theThing.append(12) affect the array here?
theThing.append(12) adds a 12 at the end of the (currently empty) array.
@AndersonGreen: The list is empty, so there is no 0th element. I just added .append(12) to give the list one. JavaScript's syntax is probably throwing you off here.
Is it possible to initialize an empty array with a particular size, then (e.g., an empty array of length 10?)
@AndersonGreen: Not really. You can do thing = [None for i in range(10)], but the list will still contain 10 elements.
|
1

You're trying to assign to a non-existent position. If you want to add an element to the list, do

theThing.append(0)

If you really want to assign to index 0 then you must ensure the list is non-empty first.

theThing = [None]
theThing[0] = 0

1 Comment

Let us suppose that I do not know the size of final array, so I cannot specify a length, and that I want to add an item in position 4, 8, and 12. append() is not the right way. How can I do that?
1

It depends on what you really need. First you have to read python tutorials about list. In you case you can use smth like:

lVals = [] 
lVals.append(0)
>>>[0]
lVals.append(1)
>>>[0, 1]
lVals[0] = 10
>>>[10, 1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.