1

People have said that the enumerate function is a hidden trick in python. I am still unsure as to what it does. The documentation just tells me that it returns an enumerate object. That doesn't exactly help me in understanding this concept.

What does enumeration do?

9
  • 3
    The documentation includes examples and a sample Python implementation of what enumerate() does. What exactly about the examples is unclear? Commented Feb 22, 2015 at 20:17
  • @MartijnPieters it talks about it using lists. Can it be used in dictionaries? Commented Feb 22, 2015 at 20:19
  • I can for example understand that the term iterator isn't immediately clear and what the __next__() method on an iterator does. But both concepts link to further documentation as well. Commented Feb 22, 2015 at 20:19
  • @MartijnPieters I think i sort of understand the first bit of code they have in the 3.4 docs Commented Feb 22, 2015 at 20:19
  • Dictionaries can be iterated over, yielding keys. enumerate() can be used on any iterable, including other enumerate() results. Commented Feb 22, 2015 at 20:20

4 Answers 4

1

Enumerate pairs the index of each element with the element:

for i in enumerate(['a', 'b', 'c']):
    print I

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

In addition, you can create your own enumeration by yourself:

enum = lambda l: zip([i for i in range(len(l))], l)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes an enumerate object is simply a generator which can be used to build an iterator. Basically, you can loop through this object as if it were a list, but in reality it's not.
0

enumerate() can be used with any iterable. It enumerates the values returned by the iterable.

3>> enumerate(['a', 'b', 'c'])
<enumerate object at 0x7f1340c44a50>
3>> list(enumerate(['a', 'b', 'c']))
[(0, 'a'), (1, 'b'), (2, 'c')]
3>> list(enumerate({'a', 'b', 'c'}))
[(0, 'c'), (1, 'b'), (2, 'a')]
3>> list(enumerate({'a':0, 'b':0, 'c':0}))
[(0, 'c'), (1, 'b'), (2, 'a')]

Note that the set and dict results are not in error; they are arbitrarily ordered, hence do not necessarily "come out" the same way they "go in". And as always, iterating over a mapping yields its keys.

Comments

0

enumerate() is a fancy tool that gives you the index of the item in the iterable as well as the item. There is nothing you can't do with you standard for loop.

lets say we have this simple list. lst = ['red', 'car', 'machine', 'go', 'cloud', 'cabbages']

An example of enumerate()

for index, item in enumerate(lst):
    print(str(item) + " Is item number: " + str(index))

without enumerate() this could might look like.

for count in range(len(lst)):
    print(str(lst[count]) + " Is item number: " + str(count))

Both produce the same output.

>>> red Is item number: 0
>>> car Is item number: 1
>>> machine Is item number: 2
>>> go Is item number: 3
>>> cloud Is item number: 4
>>> cabbages Is item number: 5

Comments

0

When iterating (for) enumerate object it returns position and thing which would usually be returned:

a = ["a", "b", "c"]
for pos, x in enumerate(a):
    print(pos, x)

Example above should be used instead of:

a = ["a", "b", "c"]
for x in range(len(a)):
    print(x, a[x])

because of code clarity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.