0

I keep getting an error saying 'list index out of range' for my change_array[i] += 0 and change_array[i] += roc. I've also tried just using a '=' and that doesn't work either.

   #define variables for a loop
   change_array = []
   roc = 0

   #iterate to find the change percentage
   for i in range (0, end_year_index+1):
       if i == 0:
          change_array[i] += 0
       else:
           roc = ((population[i] - population[i-1])/ (population[i-1]))
           change_array[i] += roc 
4
  • I think change_array should be [0 for _ in range(end_year_index+1)], if you want there to be a zero to add to at each index. You can't index arbitrarily into a Python list, or at all with an empty list. Commented Oct 26, 2015 at 20:31
  • Curiously enough you could probably do what you wanted by just making change_array a dictionary. Commented Oct 26, 2015 at 20:31
  • @JeremyKemball a defaultdict(int), certainly. Commented Oct 26, 2015 at 20:32
  • what do you expect change_array to contain initially as you have not defined it in the code above? Commented Oct 26, 2015 at 20:45

3 Answers 3

1

I assume you want to add elements to change_array, if so, you need to use change_array.append(your_element).

Sign up to request clarification or add additional context in comments.

1 Comment

answers should not be based on assumptions. do let the OP ask a clear question first.
0

change_array = [] is an empty array, therefore there is no 0 element. 0 element is the first element in arrays.

array = ["one", "two"]

array[0] is "one", array[1] is "two"

Comments

0

@Ekinydre gave you a good solution, but I just wanted to elaborate on the error you got.

When you have a list like change_array = [1, 2, 3], then change_array[i] accessed element i (lists use 0-based indices in Python). Also, += in Python is not an append operator, but an increment operator. So change_array[i] += 10 you are adding 10 to the list element at position i.

You could just append to change_array as @Ekinydre suggests, but given your code it may be safer (though less pythonic) to do something like the following:

#define variables for a loop
#create a list of 0 of length end_year_index
change_array = [0]*end_year_index
roc = 0

#iterate to find the change percentage
for i in range (0, end_year_index+1):
    if i == 0:
       change_array[i] += 0
    else:
        roc = ((population[i] - population[i-1])/ (population[i-1]))
        change_array[i] += roc

A slightly more pythonic way could look like this:

change_array = [(population[i] - population[i-1]) / population[i-1] \
                    for i in range(1, end_year_index+1)]

Note: this last solution won't have the initial 0 at the beginning of change_array.

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.