I love it because your `for` loops become much more readable β¨ if you know how to use it.
Here's a thread π𧡠on how `enumerate` will make your Python code so much better.
Let's go π
Python's `for` loops are amazing!
And yet, many people write too much stuff in their loops.
For example, if you just need to go through a list, you shouldn't be writing this π
In Python, you can just βgo through listsβ, you don't need to compute the range of the length of the list!
Instead of the above, you can just write this π
This is better because you can read it as βfor each colour in the list `l`β.
Sometimes, you need to go through an iterable and need the data AND the index.
How do you do that?
That's exactly what `enumerate` is for.
So, whenever you see this pattern π
That's when you should use `enumerate`.
I'll show an example:
For example, imagine you want to group equal letters of a word with their positions.
How'd you do it?
Think for a second β³
If I didn't know about `enumerate`, I would find all the legal indices into a string, and then access the characters βby handβ, like so π
But with `enumerate` I can clean up the code a bit...
Here is a possible implementation (not the best one!) that makes use of `enumerate` π
Notice how the `for` loop looks like:
```
for idx, letter in enumerate(word):
```
`enumerate` lets us access the indices and the corresponding letters at the same time!
How'd you read this?
I'd read that line as
βFor each index and corresponding letter in the enumeration of `word` ...β.
One thing I like about `enumerate` is that it allows me to give a DECENT name to the piece of data I'm working with.
In the example above, I used `letter` to refer to the letters.
Now, what's VERY important to note here is that `enumerate` is not a magical thing that does black magic ππ§ββοΈ
`enumerate` just takes something that you can βgo throughβ
(which is my informal way of referring to iterables)
and then produces pairs of indices and elements π
β
Wait, that doesn't look helpful π
That's because `enumerate` objects are lazy. They only give you the pairs when you need them.
Or ask for them!
Let's ask for them, then...
If we enclose the `enumerate` with a `list`, we'll get all the pairs for inspection π
Doesn't look that weird, right?
Just some tuples.
What else is there to `enumerate`..?
`enumerate` accepts a cool optional argument called `start`!
With it, you can change the... start of the counting!
What's this useful for..?
Anytime the indices don't match up with a more βnaturalβ counting!
For example, if you need to traverse lines of a file, you'll want to start counting at 1.
Or, e.g., if you truncated the data and now are picking up a later chunk that is not the beginning (index 0)...
We've already covered a lot of ground about `enumerate`, good job π₯
Let's call it a thread for now π
If you want to learn more about `enumerate` and see more code examples, you can check an article I have right here ππ mathspp.com/blog/pydonts/eβ¦
If you want to keep learning more about Python π and improving the quality of your code...
You should definitely follow @mathsppblog, I post quality content, just for you π
Also, can I ask you something? If you learn from this, retweet the thread π