Easy Python Enumerate() Function Tutorial

Easy Python Enumerate() Function Tutorial

In this post, I will talk about the Python Enumerate() function and give you a few examples on how to use it.

Python Enumerate is a Python built-in function that lets you iterate over a list and keep track of where you are in the list.

Python Enumerate Function

Here is a simple example.

First create a list.  Here is a list of some WoW classes in order of awesomeness:

awesomeList = ['paladin', 'rogue', 'priest', 'warrior', 'druid']

Now, lets say we want to iterate over the list.  We could do it like this:

for wowClass in awesomeList:
  print("Class: {}".format(wowClass))

Which gives us:

>>> awesomeList = ['paladin', 'rogue', 'priest', 'warrior', 'druid']
>>> for wowClass in awesomeList:
...   print("Class: {}".format(wowClass))
... 
Class: paladin
Class: rogue
Class: priest
Class: warrior
Class: druid
>>>

Which is pretty cool.  But we don’t have any idea where we are in the list if we are doing things with it

That’s where enumerate comes in.

Python Enumerate()

Next, lets change our code around to make use of Python Enumerate:

awesomeList = ['paladin', 'rogue', 'priest', 'warrior', 'druid']
for wowClass in enumerate(awesomeList):
   print("Class: {}".format(wowClass))
 

Running this we get:

>>> awesomeList = ['paladin', 'rogue', 'priest', 'warrior', 'druid']
>>> for wowClass in enumerate(awesomeList):
...    print("Class: {}".format(wowClass))
... 
Class: (0, 'paladin')
Class: (1, 'rogue')
Class: (2, 'priest')
Class: (3, 'warrior')
Class: (4, 'druid')
>>>

Going further we can extract the enumerated index from the WoW class like so:

>>> awesomeList = ['paladin', 'rogue', 'priest', 'warrior', 'druid']
>>> for idx, wowClass in enumerate(awesomeList):
...    print("Class: {}, Rank: {}".format(wowClass, idx))
... 
Class: paladin, Rank: 0
Class: rogue, Rank: 1
Class: priest, Rank: 2
Class: warrior, Rank: 3
Class: druid, Rank: 4
>>>

We can even change what number the index starts at.

>>> awesomeList = ['paladin', 'rogue', 'priest', 'warrior', 'druid']
>>> for idx, wowClass in enumerate(awesomeList, 1337):
...    print("Class: {}, Rank: {}".format(wowClass, idx))
... 
Class: paladin, Rank: 1337
Class: rogue, Rank: 1338
Class: priest, Rank: 1339
Class: warrior, Rank: 1340
Class: druid, Rank: 1341
>>> 

It’s cool we can do that, but I can’t think of a good IRL use case yet.  If you know of one please comment below.

How is this cool?

Using the Enumerate function lets us do some cool stuff.  One thing that comes to mind is going through a text file.  Checkout this StackOverflow question asking how to use Python Enumerate to Iterate a large text file.

Load the text file up and use Python Enumerate to iterate through the text file but keep track of where you are in the text file pragmatically.

Click here for more great Python articles on AdminTome Blog.

If you liked this post please share it using the buttons on the left or show some love by linking to it!

Thanks for reading.