Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Easy Python Enumerate() Function Tutorial

DZone's Guide to

Easy Python Enumerate() Function Tutorial

In this post we take a look into how to make effective use of Python's Enumerate() function, and see why it's so handy.

· Open Source Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

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.

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, let's 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, let's 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.

Thanks for reading.

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

Topics:
python ,function ,lists ,iterate ,code ,open source

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}