laptop on brown wooden table
Photo by XPS on Unsplash

If you have solved some sort of coding challenges online that have to do with lists, you know that you usually need to have access to indices and also elements in it.

You may also have to do that while you are working on your tasks.

Let us assume that you want to iterate through a list of strings that has the names of days of the week.

In Python, you may have had the tendency to do that using ranges and also the length of the list, like the following:

As usual, there is more than one way of doing things in Python, so let us see another one.

We can iterate through that using the enumerate() function, which is more intuitive, and more readable:

Here is the result that is going to be printed in the console once you run both of the snippets above:

Monday is day 1 of the week
Tuesday is day 2 of the week
Wednesday is day 3 of the week
Thursday is day 4 of the week
Friday is day 5 of the week
Saturday is day 6 of the week
Sunday is day 7 of the week

You can also just ignore the index entirely if you do not need it at all by replacing the first value with an underscore:

However, I would not recommend you to use enumerate() just for iterating through elements, since you can use a much simpler version:


This was another quick tip that you may have not had the chance to stumble upon until now.

I believe that it can be quite hard to write about things that nobody else has written about or covered before, so it is highly likely that you may have seen this in other articles, or other tutorials as well.

It can still be worth helping as many people as possible become just a little bit more productive than they were before.

Even if just one person learns this new tip and puts a smile on their face, I am happy with that.

I hope you find it useful.