Rodrigo πŸπŸš€ Profile picture
Take your Python 🐍 skills to the next level πŸš€!

Oct 2, 2021, 16 tweets

I love the `enumerate` Python 🐍 built-in!

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 πŸ™

🎁 Bonus:

Take a look at the code from before that implemented `group_letters`.

Can you use `defaultdict` to make it cleaner?

See you around πŸ‘‹

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling