How to use Enumerate function in python ?

In this tutorial we're going to be talking about the built-in function enumerate so what a numerate is going to do ?

How to use Enumerate function in python ?

In this tutorial we're going to be talking about the built-in function enumerate so what a numerate is going to do ?

It's going to return a tuple containing the basically the count along the way and then the object itself or the item itself that you were iterating over so you use a numerate inerrable .

first let us say we are just going to say an example

example=['left','right', 'up' ,'down']

for i in range(len(example)):

                print(i,example[i])

and the output:

0 left

1 right

2 up

3 down

This is an example of code that you might find yourself writing to produce the following like basically any time you find yourself I'm trying to think of an example where I've not been screwing up when I do this but pretty much any time you're going to say for i in range of a length of something you're probably doing it wrong okay so just keep that in mind I've done it probably no I know I've done in some of the tutorials for sure, just because like I just never use a numerator I just don't think about it but lately in my own code I've been trying to use a numerator just because it exists and you should do it.

Anyways this is not the right way to go about it instead what you should say is something like this:

for i,j in enumerate(example) :

                print(i,j)

and the output:

0 left

1 right

2 up

3 down

 I'll print the other one just to do it I guess but as you can see this one ended here the first way and the second way ,exact same output just a little cleaner  it makes a little bit more sense it's the way it's supposed to be you just do it .

That's one example of enumerate for most people that's probably all you need to do you can use enumerate over a dictionary , it's basically the exact same thing as we just did here I just don't want to make a dictionary but I will also show that you can easily because we have tuple what can you do with basically lists of tuples you can do the following you can say :

new_dict=dict(enumerate(example))

print(new_dict)

 

and the output:

{0 : 'left', 1: 'right', 2: 'up', 3: 'down'}

 

There we go so basically in this case we have created a dictionary where the key is that index value of enumerate and then the value is the thing that was in the list.

 Of course, I did not want to make a dictionary look at that we got a dictionary so we could just do this we will do this comprehension:

[print(i,j) for i,j in enumerate(new_dict)]

here we just get:

0 0

1 1

2 2

3 3

Because that is our dictionary was the keys were just 0 1 2 3 and so on ,so that was just not you're probably never going to enumerate over a dictionary that has keys like that but just showing that you can do it over a dictionary if you so choose.

So that's all on enumerate it's just a really simple addition to the Python standard library something useful mostly for when you find yourself in this case this is just always when you would use enumerate for the most part I'm sure there's lots of other times you might use it or things that you ask that you might do but for the most part this is when I found myself doing what I should have been doing enumerate .

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0