Beginner: Why am I getting this error: IndexError: list assignment index out of range

Question:

I’m a beginner with Python and wanted to make a script to collect some basketball stats from basketball-reference.com and sort the list based on a certain stat. I understand this error is thrown when you try to reference an index in a list where that index does not exist. But I’ve tried creating both a completely empty list and one with a defined range and I’m still getting that error.

CODE:

player_first_name = ["Luka", "Nikola", "Giannis", "Stephen", "Jayson"]
player_last_name = ["Doncic", "Jokic", "Antetokounmpo", "Curry", "Tatum"]
player = []

… some code not pertaining to this

for x in range(5):
    player[x] = player_first_name[x] + " " + player_last_name[x]

NOTE: I get this error if I declare player = [], player = list(), or player = [] * 5, according to what I’ve read online, all of these should have been fine. The only way I can get this error to go away is if I actually put values into each index (eg. player = ["a", "b", "c", "d", "e"]

As said before, I’ve tried declaring the player list as:

player = []
player = [] * 5
player = list()

All of these cases resulted in the error.

Asked By: maxpower8888

||

Answers:

player = [] is an empty list. If you want to assign values to this list you have to use append or any other method. This method will give you error:

for x in range(5):
    player[x] = player_first_name[x] + " " + player_last_name[x]

#IndexError: list assignment index out of range

You cannot simply do a for loop and assign value since it is an empty list. the correct way would be:

for x in range(5):
    player.append(player_first_name[x] + " " + player_last_name[x])
print(player)


#['Luka Doncic', 'Nikola Jokic', 'Giannis Antetokounmpo', 'Stephen Curry', 'Jayson Tatum', 'Luka Doncic', 'Nikola Jokic', 'Giannis Antetokounmpo', 'Stephen Curry', 'Jayson Tatum']
Answered By: Talha Tayyab

Playing around with an online python interpreter a bit at https://replit.com/languages/python3 – you can do that locally, but it’s convenient for quickly checking bits of syntax and learning exactly what they do.

Let’s explore things one by one:

x = []
x[1]= 2
print(x[1])

results in:

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

Which makes sense – we’re trying to assign to an index that isn’t there. list() will do the same. Checking the []*5 thing, we see:

print([]*5) outputs []

So that’s the same thing.

print([1]*5) outputs in [1, 1, 1, 1, 1]

So it looks like multiplying a list by a number just repeats the list – repeating an empty list is just an empty list.

x = []
x.append(1)
print(x)

gives us what we want, though, so that’s one approach. Personally I would get more complex and do something like:

x= list(map(lambda x : x * 2, range(5)))
print(x)

which results in:

[0,2,4,6,8]

for reasons I’ll explain below. Here, the map function is taking an function that can be performed on items and a list, and a list, and resulting a list created by applying the function to each element of the list. For your case, that would look like

players = list(map(lambda x : layer_first_name[x] + " " + player_last_name[x], range(5))

The reason I would to that is that it is immutable – values do not change. It’s not really important in this case, but that’s a good habit to get into. There’s an entire paradigm of programming built around that, as they’re often easier to analyze (both for humans and machines.) I won’t proselytize too much, but at minimum I will say it’s good to use immutable constructions by default when it’s convenient to do so.

Answered By: Edward Peters
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.