Python error messages are concrete and easy to understand "ga" (IndexError: list index out of range with element [0])

Purpose

I don't have enough experience to compare with anything, ** Python error messages are concrete and easy to understand ** I feel that. Therefore, the error can be read not only at a glance but also seriously. I feel that it is a shortcut for error resolution.

However, there are times when I don't understand for a moment, so ** Let's calm down on the assumption that you should understand ** This is an article to show such feelings.

Index Error: list index out of range on element [0]

The meaning of Index Error: list index out of range ** in the supplementary ** element [0] is When it is said that it is out of range, why is element [0] out of range? is what it means.

Example error message

The code shown here is for issuing an error message. It's not code that can make exactly the same mistakes. I repeat. This is the code to issue an error message. (However, I've seen the same error in a slightly more confusing code.)

Error 1

** IndexError: list index out of range ** is displayed. The code is below. The code below doesn't make sense, (However, in the following, if b is ** getting a list by the return value of the function **, I think that you may meet.)

index_err1.py


a = [1, 2, 3]
b = []

print("a[0]",a[0])
print("b[0]",b[0])

The error display is as follows.

Traceback (most recent call last):
  File "list_err1.py", line 5, in <module>
    print("b[0]",b[0])
IndexError: list index out of range

Listing b just says it doesn't even [0], Again, I don't write this code, In many cases, b gets a list from the return value of the ** function. **I thought.

point

** IndexError: list index out of range ** should be set unless the Index is specified incorrectly. There are cases where the original object is not created properly. ** In many cases, such as when you get a list by the return value of a function. ** **

Summary

I think it's a natural result in terms of composing the error message, but I felt it was easy to understand. .. ..

Related (person)

Things that are not directly related

Use python without stress! (Become familiar with generator. It seems to be since1975.)

Use python without stress! (In Python, everything is implemented as an object) Use python without stress! (Close to Pylint) Use python without stress! (Expression and Statement) Learn Python carefully using both English and Japanese.

from now on

If you have any comments, please let us know. : candy: Will study,,,,

Recommended Posts

Python error messages are concrete and easy to understand "ga" (IndexError: list index out of range with element [0])
Python error messages are concrete and easy to understand "ga" (SyntaxError on the closing side of triple "" "comments)
Python error messages are specific and easy to understand "ga" (... AAA yyy BBB)
Python error messages are specific and easy to understand "ga" (before that, a colon (:) and a semicolon (;))
python: Tips for displaying an array (list) with an index (how to find out what number an element of an array is)
OSX MySQL-python 1.2.5 error handling IndexError: string index out of range
[Python] Understand the content of error messages
Python list comprehensions that are easy to forget
List of Python code to move and remember
How to identify the element with the smallest number of characters in a Python list?
How to count the number of occurrences of each element in the list in Python with weight
Easy partial download of mp4 with python and youtube-dl!
Visualize the range of interpolation and extrapolation with python
Return the image data with Flask of Python and draw it to the canvas element of HTML
AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression
Easy to use Nifty Cloud API with botocore and python
python memo: enumerate () -get index and element of list at the same time and turn for statement
Python> list> append () and extend ()> append: list is added | extend: list elements are added | + = to add list
Regular expressions that are easy and solid to learn in Python
I tried to create a list of prime numbers with python
[Completed version] Try to find out the number of residents in the town from the address list with Python