List-index-out-of-range
08 Oct 2020Hey there!
From today onwards, I will log my daily progress/goals in the blog. I recently read an amazing blog of a friend of mine that had a daily progress section. The motive behind:
If I’ll put it in public, I’ll do it, may be not now, but certainly I will.
What are your views on this, let me know in the comments section!
First of all the daily progress/goals section:
- Setup the About section of the portfolio
- Update the readme section of my Github Profile
- Design a workflow in github to fetch my top 5 blogs and show them on the ReadMe section.
- Separate Blogs on the implementation of topics mentioned above. 😆
Okay! Well now coming to the title, IndexError: list index out of range.So how often you encounter this 😆. Or how often you end up spending hours on something like this.
I'm pretty sure, many time would be the answer.
Actually, I faced an issue today where I was running a loop like this:
for i in range(0,len(my_list)):
stuff_done_here
And in the loop, I was deleting certain entries from the original list my_list. So during the iterations, the length of the list actually got reduced and hence, I was getting this: IndexError: list index out of range
Though it seems to be a straight-forward issue, yet it took me about an hour error to find the root-cause.😆
The FIX:
for index,item in enumerate(my_list):
stuff_done_here
Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object. This enumerate object can then be used directly in for loops or be converted into a list of tuples using list() method.
Find out more on this here
Lets catch up in the next post.
Till then O/