# Index Out of Range of Numeric List Values

Hello,

a = [10,15,25,30,35,40,45,50,55]

print(f’The price for the first three entries is {a[0+1+2]}’)
print(f’The price for the middle three entries is {a[3+4+5]}’)
print(f’The price for the last three entries is {a[6+7+8]}’)

On the above code, am trying to sum the first, middle and last three entries. The first three entries ran successfully by giving the value of 30 but the middle and last three entries received an err “List Index Out of Range”

Eagle Eye

Hello, @radeyeye, and welcome to Python Foundation Discourse!

You need to index each item individually, as follows:

``````print(f'The price for the middle three entries is {a[3]+a[4]+a[5]}')
``````

Please format code when posting it, so that it is easier to read, and so that details such as quotes and indentation appear properly. One way to do this is to select the code after it is pasted into your post, and then to click the `</>` button above the editing window.

1 Like

Thanks Quercus,

That solved my prob for both middle and last entries. Much appreciated.

Eagle Eye

1 Like

Whenever you are surprised by how Python evaluates an expression, I find it helpful to write it down on a piece of paper and carefully work through what Python will do, one operation at a time, substituting the result of each operation into the original expression. It’s very much like math in elementary school (in many schools, I hope) – operations in parentheses go first, `*` before `+`, etc. – with a few extras (like calling functions).
For example:

``````print(f'The price for the first three entries is {a[0+1+2]}')
# Zero plus one is one:                             ╰┬╯
print(f'The price for the first three entries is {a[ 1 +2]}')
# One plus two is three:                            ╰─┬─╯
print(f'The price for the first three entries is {a[  3  ]}')
# Item number three (fourth item) of a is 30:     ╰──┬───╯
print(f'The price for the first three entries is {  30    }')
# The 30 gets inserted into the string:          ╰┬───────╯
print( 'The price for the first three entries is 30')
# The `print` function is called, printing the resulting string.
``````

vs.

``````print(f'The price for the first three entries is {a[0]+a[1]+a[2]}')
# Item number zero of a is 10:                    ╰─┬╯
print(f'The price for the first three entries is { 10 +a[1]+a[2]}')
# Item number one of a is 15:                          ╰─┬╯
print(f'The price for the first three entries is { 10 + 15 +a[2]}')
# Item number two of a is 25:                               ╰─┬╯
print(f'The price for the first three entries is { 10 + 15 + 25 }')
# Ten plus fifteen is twenty-five:                 ╰──┬──╯
print(f'The price for the first three entries is {    25   + 25 }')
# Twenty-five plus twenty-five is fifty:              ╰───┬───╯
print(f'The price for the first three entries is {       50     }')
# The 50 gets inserted into the string:          ╰┬─────────────╯
print( 'The price for the first three entries is 50')
# The `print` function is called, printing the resulting string.
``````

(I believe there are also tools to visualize this, but pen & paper works surprisingly well.)

Some programmers use a similar technique called rubber-duck programming, where they force themselves to walk through their code and explain it in simple terms.

6 Likes

Thanks Petr, this is an awesome illustration. It actually showed what I was trying to do vs the solution that Quercus provided. True, good programming involved breaking everything down and try to put it together piece by piece, rather than want to solve the whole code at once.

1 Like