0

I just can't understand why my list is out of range.

I've been stuck on this problem forever now and can't wrap my head around what the problem might be.

Obviously I'm new into programming.

def list(self,filename):
    with open(filename, "r") as listTeams:
        teamCount = 0
        indexList = []
        unsortedList = []
        lines = listTeams.readlines()

        for line in open(filename):
            teamCount += 1

        listTeams.seek(0)

        for i in range(0, teamCount):
            tempStr = listTeams.readlines(i)
            tempStr = "".join(tempStr)
            indexList = tempStr.split(" ")
            for i in range(0,i):
                print(i)
                unsortedList[i] = Team(indexList[0], indexList[1], indexList[2], indexList[3], indexList[4], indexList[5], indexList[6])
                print(unsortedList[i])
    return

I get this error message:

unsortedList[i] = Team(indexList[0], indexList[1], indexList[2], indexList[3], indexList[4], indexList[5], indexList[6])
IndexError: list index out of range
6
  • How many columns are there in the file? indexList[6] requires every line to have at least 7 fields. Commented Nov 24, 2016 at 3:34
  • 1
    unsortedList = [] just creates an unsized list. It looks like you want to use .append(Team(...)) instead. Commented Nov 24, 2016 at 3:34
  • 1
    Not related to the question, but I would really not recommend naming a function list, since that is a built-in function in python. Commented Nov 24, 2016 at 3:35
  • Why are you using listTeams.readlines(i) to read each line? You already read them all into the list lines. You can just do for tempStr in lines. Commented Nov 24, 2016 at 3:35
  • You can also do teamCount = len(lines), you don't need to read the file again and count the lines. Commented Nov 24, 2016 at 3:36

2 Answers 2

1

You can't assign values to List items which don't exist. You can use one of the two methods to solve this problem. One, you can use this command unsortedList.append(Team(indexList[0], indexList[1], indexList[2]). or Second, you can create a list apriori which contains as many zeros as your list will contain by using the command unsortedList= numpy.zeros(i), it will create a list with i number of zeros then you can replace those zeros using your code.

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

Comments

0

you can't assign to a list element that doesn't already exist, you can use append method

unsortedList.append(Team(indexList[0], indexList[1], indexList[2], indexList[3], indexList[4], indexList[5], indexList[6]))

also check the how many items in the array before accessing by index len(indexList) give you the number of items in the array, check that is more than 6 before you get the items 0 to 6 in the indexList

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.