Skip to content
Home » The enumerate Function in Python

The enumerate Function in Python

Spread the love

Today we’ll be talking about the enumerate function. We use it, for example, if we need both the elements of a sequence and their indices.

The enumerate function generally takes one or two parameters. The first parameter is the iterable that you want to iterate over and the second, optional parameter is the counter start value, so the initial value, starting from which the counting will be carried on.

Let’s define a list of students to illustrate how the function works:

>>> students = ['Sam', 'Joe', 'Bill', 'Annie']

Here they are, waiting to be enumerated.

students

Now let’s use the list to create an enumerate:

>>> enumerateStudents = enumerate(students)

You can check the type of enumerateStudents. It’s an enumerate.

>>> type(enumerateStudents)
<class 'enumerate'>

Using the list and tuple functions, you can convert an enumerate to a list or tuple respectively. Let’s use the former function first:

>>> list(enumerateStudents)
[(0, 'Sam'), (1, 'Joe'), (2, 'Bill'), (3, 'Annie')]

And now suppose we need some more students and we want them to be counted from index 4. This is what the second parameter in the enumerate function is for. Let’s create a list with the other students and then an enumerate:

>>> more_students = ['Jane', 'Martha', 'Lucy']
>>> enumerateMoreStudents = enumerate(more_students, 4)

You can see the three students here.

students

This time let’s convert the enumerate to a tuple:

>>> tuple(enumerateMoreStudents)
((4, 'Jane'), (5, 'Martha'), (6, 'Lucy'))

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

The enumerate Function in Loops

In a similar way as how we iterate over the keys and values of a dictionary, you can use the enumerate function to retrieve the indices and their corresponding values from sequences:

>>> name = "David"
>>> for i, v in enumerate(name):
...     print("{} - {}".format(i, v))
... 
0 - D
1 - a
2 - v
3 - i
4 – d

In the example above we used the enumerate function with just one argument, so the counting starts at 0. But you can set the initial index to a different value:

>>> for i, v in enumerate(name, 101):
...     print("{} - {}".format(i, v))
... 
101 - D
102 - a
103 - v
104 - i
105 - d

***

OK, that’s it for today.

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

If you would like to see more content like this, make sure to subscribe to my mailing list and newsletter. I have a present for all subscribers – my 113-page Introduction to numpy that covers all the basics of the numpy module. This is the module you should start with if you’re thinking about science or data science with Python.

freebie

***

Do you know any interesting use cases for the enumerate function? If you do, write them down in the comments.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply