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 πŸ‘‹

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Rodrigo πŸπŸ“

Rodrigo πŸπŸ“ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mathsppblog

2 Oct
πŸ› πŸ‘€ On the 23/09 I challenged myself to go from 2.95k followers to 4k by the end of the month 😱

That was +1.05k followers (+36%) in 7 days.

On the 1st Oct I woke up to a little over 6k followers πŸŽ‰πŸ”₯ that's INSANE!

Thanks for your support!

Here's a recap of these days πŸ‘‡πŸ§΅
First off, for reference, here is the original tweet with the public challenge:

In the original tweet I said that my strategy would be to publish a high-value thread each day.

For my challenge to be met, I had to grow β‰ˆ4% each day.

Here's a breakdown of the number of followers I woke up to each day, with the % change relative to the previous day.
Read 13 tweets
30 Sep
β€œBe consistent”, they say.

But why?

Have you ever heard of the compounding effect?

Or exponential growth?

I'll show you how all these are connected, with real-life examples πŸ‘‡πŸ§΅ Image
I'll let you in on a little secret: I have a personal goal on Twitter.

The goal is to grow by 1% every day.

That's just that.

Every day, I want my follower count to increase by 1%

If you are anything like me, you might be thinking:
β€œIsn't 1% too little? You're gonna take ages to grow!”

Well...

Is 1% that little?

I'll show you it isn't!

First, here's a screenshot of my growth in the beginning of my Twitter journey πŸ‘‡

Let's work with those numbers... Image
Read 32 tweets
29 Sep
I challenged you πŸ†...

You delivered πŸ’ͺ!

I asked you to implement the sign function in Python 🐍.

Now I'll go over some alternatives and tell you what I like βœ… and dislike ❌ about them.

I'll also tell you which one I think is the best, most Pythonic ✨ one.

πŸ‘‡πŸ§΅
By the way, for reference, here is the original challenge:

Let's start with the β€œbasic” `if: ... elif: ... else: ...` approach first.

Gets the job done, is simple, and is easy to ready.

Funnily enough, the only one to share that solution was a _very_ seasoned Python 🐍 programmer, author and trainer, @dabeaz πŸ™ƒ

Now, a couple of notes:
Read 28 tweets
28 Sep
Interested in improving your Python 🐍 skills?

πŸ€” If you are, have you learned about conditional expressions?

Here is a MEGA thread πŸ‘‡πŸ§΅ teaching you almost everything there is to know about conditional expressions!

I will include a bunch of examples πŸ–Ό, so don't worry!
This is the follow-up to my experiment from earlier today.

I tried teaching you about conditional expressions without words 😢.

Here is the original thread:

Alright, so conditional expressions aren't that hard, really.

It is just an expression: a piece of code that evaluates to a result.

But then, it is tied to a condition: depending on whether the condition is truthy or falsy, the final result changes.

Check the function below:
Read 30 tweets
28 Sep
πŸ€” If you are learning Python 🐍, I want to do an experiment:

Here's a thread πŸ‘‡πŸ§΅ teaching you conditional expressions.

BUT, I will teach you with NO words 😢

I will only show code snippets and examples.

Check the thread out carefully πŸ” and then give me your feedback!
By the way, if you want to learn something from this thread...

I'm not going to lie, you'll have to focus πŸ”.

Also, what's the rationale for this crazy πŸ€ͺ thread?

By reading code and examples, you see first-hand how the feature is used and where it comes in handy.

Let's go:
Read 17 tweets
26 Sep
Have you heard that Python 🐍 3.10 will be released soon?

Are you ready for when it drops πŸ’£πŸ”₯?

Python 3.10 will ship with the new ✨ match statement, and I created a cheatsheet just for that.

This is also a thread πŸ‘‡πŸ§΅ that breaks it down and explains everything:
The cheatsheet is free and teaches you 12+ things about the new `match` statement.

From the basics, to object destructuring, to wildcards, among other things.

Get it from the link πŸ‘‡, and keep reading below to learn all about the `match` statement.

mathspp.gumroad.com/l/cheatsheet_m…
In its most basic form, a `match` statement kind of resembles an `if` statement.

That's what most of the β€œswitch” or β€œmatch” statements in other languages do: a series of `if` / `elif` / `else` statements.

Even a basic `case` is powerful: use `|` to separate multiple options. Image
Read 23 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(