Skip to main content

Posts

Showing posts with the label The Crazy Programmer Solve IndexError: list index out of range in Python Solve IndexError: list index out of range in Python The Crazy Programmer

Solve IndexError: list index out of range in Python Pratik Sah The Crazy Programmer

Hello readers, welcome back to a yet another post of The Crazy Programmer. Today I’ll be discussing one of the common error programmers make when they start with programming and that is IndexError . Now before talking about this error, let’s try to see what an Index is and why this error occurs. An index is a location of an item in an Array or a List and in most of the programming language, it starts from 0. So when we count the elements in a list, we’ll count them from 1 but when we’ll try to access the elements of the list, we’ll start our index from 0. Let us understand this with an example. Please don’t skip the comments. color = ['red', 'green', 'blue', 'black', 'white'] # index 0 1 2 3 4 # here we can see that we have 5 colors in our color list # but when we'll try to print these items from our list # we can't get color[1] as red # this is because we may find red to be the first element # but techni