What does enumerate() mean?
Asked Answered
L

8

349

What does for row_number, row in enumerate(cursor): do in Python?

What does enumerate mean in this context?

Legit answered 4/3, 2014 at 12:8 Comment(2)
docs.python.org/2/library/functions.html#enumerateTurbo
docs.python.org/3/library/functions.html#enumerateWendellwendi
C
538

The enumerate() function adds a counter to an iterable.

So for each element in cursor, a tuple is produced with (counter, element); the for loop binds that to row_number and row, respectively.

Demo:

>>> elements = ('foo', 'bar', 'baz')
>>> for elem in elements:
...     print elem
... 
foo
bar
baz
>>> for count, elem in enumerate(elements):
...     print count, elem
... 
0 foo
1 bar
2 baz

By default, enumerate() starts counting at 0 but if you give it a second integer argument, it'll start from that number instead:

>>> for count, elem in enumerate(elements, 42):
...     print count, elem
... 
42 foo
43 bar
44 baz

If you were to re-implement enumerate() in Python, here are two ways of achieving that; one using itertools.count() to do the counting, the other manually counting in a generator function:

from itertools import count

def enumerate(it, start=0):
    # return an iterator that adds a counter to each element of it
    return zip(count(start), it)

and

def enumerate(it, start=0):
    count = start
    for elem in it:
        yield (count, elem)
        count += 1

The actual implementation in C is closer to the latter, with optimisations to reuse a single tuple object for the common for i, ... unpacking case and using a standard C integer value for the counter until the counter becomes too large to avoid using a Python integer object (which is unbounded).

Complementary answered 4/3, 2014 at 12:10 Comment(0)
A
98

It's a builtin function that returns an object that can be iterated over. See the documentation.

In short, it loops over the elements of an iterable (like a list), as well as an index number, combined in a tuple:

for item in enumerate(["a", "b", "c"]):
    print item

prints

(0, "a")
(1, "b")
(2, "c")

It's helpful if you want to loop over a sequence (or other iterable thing), and also want to have an index counter available. If you want the counter to start from some other value (usually 1), you can give that as second argument to enumerate.

Africah answered 4/3, 2014 at 12:12 Comment(2)
if you add a second variable to item then you can remove the parenthesis :DViguerie
Pedantic: It's a function returning an iterator, not a generator. Generators are a specific class of user-defined functions (using yield/yield from, or generator expressions, which implicitly yield); enumerate isn't a generator. For all duck-typing related purposes there is no difference, but explicit type checking and the Python language docs only uses the term "generator" for one purpose, which doesn't cover enumerate.Germander
M
25

I am reading a book (Effective Python) by Brett Slatkin and he shows another way to iterate over a list and also know the index of the current item in the list but he suggests that it is better not to use it and to use enumerate instead. I know you asked what enumerate means, but when I understood the following, I also understood how enumerate makes iterating over a list while knowing the index of the current item easier (and more readable).

list_of_letters = ['a', 'b', 'c']
for i in range(len(list_of_letters)):
    letter = list_of_letters[i]
    print (i, letter)

The output is:

0 a
1 b
2 c

I also used to do something, even sillier before I read about the enumerate function.

i = 0
for n in list_of_letters:
    print (i, n)
    i += 1

It produces the same output.

But with enumerate I just have to write:

list_of_letters = ['a', 'b', 'c']
for i, letter in enumerate(list_of_letters):
    print (i, letter)
Mycosis answered 24/4, 2018 at 13:37 Comment(0)
L
23

As other users have mentioned, enumerate is a generator that adds an incremental index next to each item of an iterable.

So if you have a list say l = ["test_1", "test_2", "test_3"], the list(enumerate(l)) will give you something like this: [(0, 'test_1'), (1, 'test_2'), (2, 'test_3')].

Now, when this is useful? A possible use case is when you want to iterate over items, and you want to skip a specific item that you only know its index in the list but not its value (because its value is not known at the time).

for index, value in enumerate(joint_values):
   if index == 3:
       continue

   # Do something with the other `value`

So your code reads better because you could also do a regular for loop with range but then to access the items you need to index them (i.e., joint_values[i]).

Although another user mentioned an implementation of enumerate using zip, I think a more pure (but slightly more complex) way without using itertools is the following:

def enumerate(l, start=0):
    return zip(range(start, len(l) + start), l)

Example:

l = ["test_1", "test_2", "test_3"]
enumerate(l)
enumerate(l, 10)

Output:

[(0, 'test_1'), (1, 'test_2'), (2, 'test_3')]

[(10, 'test_1'), (11, 'test_2'), (12, 'test_3')]

As mentioned in the comments, this approach with range will not work with arbitrary iterables as the original enumerate function does.

Lafayette answered 18/7, 2018 at 13:43 Comment(1)
The reason the other answer mentions itertools is that your range approach only works with containers. enumerate works with arbitrary iterables; if you want to iterate a file, for lineno, line in enumerate(myfile): works, but you couldn't do range(len(myfile)) because the length isn't known until you reach EOF.Germander
A
14

The enumerate function works as follows:

doc = """I like movie. But I don't like the cast. The story is very nice"""
doc1 = doc.split('.')
for i in enumerate(doc1):
     print(i)

The output is

(0, 'I like movie')
(1, " But I don't like the cast")
(2, ' The story is very nice')
Autacoid answered 5/2, 2018 at 13:23 Comment(0)
F
0

I am assuming that you know how to iterate over elements in some list:

for el in my_list:
    # do something

Now sometimes not only you need to iterate over the elements, but also you need the index for each iteration. One way to do it is:

i = 0
for el in my_list:
    # do somethings, and use value of "i" somehow
    i += 1

However, a nicer way is to user the function "enumerate". What enumerate does is that it receives a list, and it returns a list-like object (an iterable that you can iterate over) but each element of this new list itself contains 2 elements: the index and the value from that original input list: So if you have

arr = ['a', 'b', 'c']

Then the command enumerate(arr) returns something like:

[(0,'a'), (1,'b'), (2,'c')]

Now If you iterate over a list (or an iterable) where each element itself has 2 sub-elements, you can capture both of those sub-elements in the for loop like below:

for index, value in enumerate(arr):
    print(index,value)

which would print out the sub-elements of the output of enumerate.

And in general you can basically "unpack" multiple items from list into multiple variables like below:

idx,value = (2,'c')
print(idx)
print(value)

which would print

2
c

This is the kind of assignment happening in each iteration of that loop with enumerate(arr) as iterable.

Forsterite answered 11/12, 2020 at 21:40 Comment(0)
T
0

the enumerate function calculates an elements index and the elements value at the same time. i believe the following code will help explain what is going on.

for i,item in enumerate(initial_config):
     print(f'index{i} value{item}')
Tevis answered 31/3, 2022 at 2:19 Comment(0)
L
0

The enumerate function is used to keep count of iterations. It takes in two parameters: an iterable and a start index as seen here: enumerate(iterable, start=1)

If no start index is specified, it starts at 0 by default. It returns the index and the corresponding element in the iterable. Consider the following example:

for i, j in enumerate(range(5)):
    print(i,'=',j)

The output will be:

0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
Lindemann answered 12/11, 2023 at 23:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.