Python: list index out of range even if I am looking at element with index 0
sorry if this question has already been asked but I can't find any answer that solves my problem. I am using Python 3.8 with PyCharm on Mac (if that information can help). I just started learning python and have a solid C and MatLab background. My goal here is to read some information about train stations from a file in the format and then ask the user for a station and give out the names of the stations that are connected by trains. Here is my code:
fin = open('trains', 'r') string = fin.read() lines = string.split('\n') print(lines) station = input("Insert station name\n") from_station =  #stations from which trains arrive at the user's station to_station =  #stations to which trains arrive from user's station for i in range(0,len(lines)): words = lines[i].split() for i in range(0,4): print(words[i]) #put to check if the words list actually stores the different words if words == station: to_station.append(words) if words == station: from_station.append(words) print("Trains arriving from stations: ") print(from_station) print("Trains going to stations: ") print(to_station) fin.close()
I keep getting the Index out of bounds error for
print(words[i]) in line 17 even if my complier (or interpreter) manages to print the right informartions without any problem.
I cannot manage to compile the code after the end of the for.
Thank you in advance for your help
EDIT: Even if I make that correction you suggested - I didn't notice in the inner loop that mistake - I still keep getting that error. I get that error even if I remove that inner loop altogether.
issue is in this line
words = lines[i].split()
you need to check the
len(words) each time and need to confirm that
len(words) is in bound of your indices range
exactly viewing your data can resolve the issue
Python IndexError: List Index Out of Range (How to Fix This Stupid , One frequent bug in Python is the IndexError: list index out of range . For example, if you try to access the list element with index 100 but your lists consist only Let's have a look at an example where this error arises: It's a common mistake: The index of the third element is 2 because the index of the first list element is 0. Here index is 3 which is equal to length of list so you can't access it. If you have a list with 3 items, the last one is MyList because the way Python indexing works is that it starts at 0. The IndexError: "Index Out of Range" can occur for all ordered collections where you can use indexing to retrieve certain elements.
Use another variable in the inner loop other than 'i'.
Indexerror: list Index Out of Range in Python, In python “list index out of range” error occurs when we try to access an working with lists in Python, you have to know the index of the list elements. And we know that the index of a list starts from 0 that's why in the list, the� The final step  just takes the first element, thus returning 'sss', but would have returned a if there was such an element` – vlad-ardelean Apr 1 '15 at 15:57 5 an interesting way to achieve this. a bit on the unreadable side.
The problem comes from your inner loop and also the iterator on the list words. You may have a list of two words then can have a Index out of bounds error.
fin = open('trains', 'r') string = fin.read() lines = string.split('\n') print(lines) station = input("Insert station name\n") from_station =  #stations from which trains arrive at the user's station to_station =  #stations to which trains arrive from user's station for i in range(0,len(lines)): words = lines[i].split() for j in range(0,len(words)): print(words[j]) #put to check if the words list actually stores the different words if words == station: to_station.append(words) if words == station: from_station.append(words) print("Trains arriving from stations: ") print(from_station) print("Trains going to stations: ") print(to_station) fin.close()
"List index out of range error" - Python, I get the error “IndexError: list index out of range” but I don't see how. My second loop I should be looping through a list that starts at zero a string element with numbers separated by spaces (or even a string? - if This will cut in place of spaces, so you will have a list of string values representing numbers. Generally it means that you are providing an index for which a list element does not exist. E.g, if your list was [1, 3, 5, 7], and you asked for the element at index 10, you would be well out of bounds and receive an error, as only elements 0 through 3 exist.
Why do I get a list index out of range error?, still in the battleship course I have a question : if guess_row not in range(6) or guess_col not in range(6): #i looked at hint for if a list has 5 elements (length of 5), the highest index is 4 (since else: if guess_row not in range(0,5) or guess_col not in range(0,5): print "Oops, that's not even in the ocean. Consider a list of integers: >>a= range(5) >>a [out]: [0,1,2,3,4] It has indices from 0,1,2,3,4. For example: If you try: >> a , a, a , a[-2] [OUT]: 0 1 4 3 When you try to access the index of the list of ‘a’ which is not from the above i
What does list index out of range mean?, In python list is mutable, so the size is not fixed. If you try to access the empty or None element by pointing available index of the list, Then you will get the “List index out of even. IndexError: list index out of range. So in above code we have defined even list that contain 4 element with starting index 0 and last index 3. Python List index () The index () method returns the index of the specified element in the list. The syntax of the list index () method is: list.index (element, start, end)
For Loop With Range of Length to Iterate Through List in Python. You can add a range of length with for loop of Python. With range, you can print list elements between the given range. You can print the element in the range whatever you want. Below are the different range of length to print the elements of the list in Python.