Today, we’re making our way through our in-depth Python Exception Handling series. In this article, we’ll discuss the Python IndexError: Index Out of Range. If you read The 2022 Airbrake Error Data Report, you'll see that 28% of Python projects contain IndexErrors. That's why it's important to know how to resolve this error.
Throughout this post, we’ll examine the Index Out of Range error in detail, starting with where it sits in the larger Python Exception Class Hierarchy, why it occurs, and how to fix it.
Like most Python errors, the IndexError derives from the LookupError class, which is inherited from the built-in BaseException.
Within the LookupError class, there are two Errors listed:
Now that you have an idea where the error is within the Python hierarchy, let’s continue on to what it is.
What is the Index Out of Range Error?
Python supports the indexing of list elements. This helps access iterable items and perform operations on them, such as printing or looping through elements. But, if you mention an index in your code that is outside the range of the list, Python will throw an IndexError telling you that the list index is out of range.
Here’s an example: When we try to access an element using an index that is outside the range of that list, you’ll receive a “List index out of range” error. In this case, the print(a) refers to a list that doesn’t exist.
Something to keep in mind: A reference lists elements by their numeric position beginning with zero. Using the same example above, you would reference the first element with an index of 0 (e.g. a) and to index the last element, you would use a.
How to Handle an IndexError Exception
The only way to avoid this error is to mention the indexes of list (iterable) elements properly.
A try-except block can also be used to find and handle an IndexError. If an exception occurs during execution, the exception will be caught up in the except block.
Want to learn more about IndexErrors? Check out our blog post, Python Exception Handling: IndexError where we go into depth about this error.
Why Use Airbrake
Code riddled with IndexErrors? Uncover and resolve errors such as an Index Out of Range, quickly. Airbrake Error & Performance Monitoring will tell you exactly where the error resides in your code, right down to the broken line.
But, you don’t need to take our word for it. Create a Free Airbrake Dev Account and, for the first 30 days, you’ll have unlimited errors and events.
Leo, sisi ni kufanya njia yetu kwa njia ya kina yetu Python Exception Handling mfululizo. Katika makala hii, tutazungumzia Python IndexError: Index Out of Range. Kwa mfano, katika ripoti ya Airbrake Error Data ya 2021, asilimia 28 ya miradi ya Python ina makosa ya Index. Hiyo ndiyo sababu ni muhimu kujua jinsi ya kutatua kosa hili. Katika chapisho hili, tutaangalia kosa la Index Out of Range kwa undani, kuanzia na mahali inapoishi katika Utaratibu mkubwa wa Darasa la Python Exception, kwa nini hutokea, na jinsi ya kuirekebisha. Kama makosa mengi ya Python, IndexError inatokana na darasa la LookupError, ambalo linarithiwa kutoka kwa BaseException iliyojengwa. Ndani ya LookupError darasa, kuna makosa mawili waliotajwa: Sasa kwamba una wazo ambapo kosa ni ndani ya Python utaratibu, hebu kuendelea na nini ni. Ni nini Index nje ya Range makosa? Python inasaidia indexing ya orodha ya vipengele. Hii husaidia kupata vitu iterable na kufanya shughuli juu yao, kama vile kuchapisha au looping kwa njia ya vipengele. Lakini, kama kutaja index katika kanuni yako ambayo ni nje ya mbalimbali ya orodha, Python itatupa IndexError kukuambia kwamba orodha index ni nje ya mbalimbali. Hapa ni mfano: Wakati sisi kujaribu kupata kipengele kwa kutumia index ambayo ni nje ya mbalimbali ya orodha hiyo, utapata <unk>List index nje ya mbalimbali<unk> kosa. Katika kesi hii, print(a) inahusu orodha ambayo haipo. Jambo la kukumbuka: Marejeo huorodhesha vitu kwa nafasi zao za tarakimu zinazoanza na sifuri. Kwa mfano, kama ilivyoelezwa hapo juu, unaweza kuorodhesha kipengele cha kwanza na index ya 0. A) na index kipengele cha mwisho, unaweza kutumia a. Jinsi ya kushughulikia IndexError Exception Njia pekee ya kuepuka kosa hili ni kutaja faharisi ya orodha (iterable) vipengele vizuri. block jaribu-isipokuwa pia inaweza kutumika kupata na kushughulikia IndexError. Kama exception hutokea wakati wa utekelezaji, exception itakuwa kukamatwa katika except block. Unataka kujifunza zaidi kuhusu IndexErrors? Angalia chapisho letu la blogi, Python Exception Handling: IndexError ambapo tunaenda kwa kina juu ya kosa hili. Kwa nini kutumia Airbrake Code riddled na IndexErrors? Kufunua na kutatua makosa kama vile Index nje ya Range, haraka. Airbrake makosa na utendaji ufuatiliaji nitakuambia hasa ambapo kosa anaishi katika kanuni yako, haki chini ya mstari kuvunjwa. Lakini, huna haja ya kuchukua neno letu kwa ajili yake. Kuunda akaunti ya bure ya Airbrake Dev na, kwa siku 30 za kwanza, utakuwa na makosa na matukio yasiyo na kikomo.