0

What is <enumerate object at 0x000000000302E2D0> mean?

>>> my_list = ['apple', 'banana', 'grapes', 'pear']
>>> enumerate(my_list)
<enumerate object at 0x000000000302E2D0>

I try Google and still don't understand why we have <enumerate object at 0x000000000302E2D0>. Can you help me with this problem?
Thank you.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445

2 Answers2

2

It returns an enumerate object, which is an iterator. It does not actually show you what it contains until you specifically ask it to. One way to do this is to force it to be a list.

>>> my_list = ['apple', 'banana', 'grapes', 'pear']
>>> a = enumerate(my_list)
>>> a
<enumerate at 0x7ffff27d0630>
>>> list(a)
[(0, 'apple'), (1, 'banana'), (2, 'grapes'), (3, 'pear')]

You can also iterate over an enumerate object in a for loop.

Check out this question for more information about iterators.

Community
  • 1
  • 1
SethMMorton
  • 45,752
  • 12
  • 65
  • 86
1

From enumerate.__doc__:

enumerate(iterable[, start]) -> iterator for index, value of iterable

Return an enumerate object.  iterable must be another object that supports
iteration.  The enumerate object yields pairs containing a count (from
start, which defaults to zero) and a value yielded by the iterable argument.
enumerate is useful for obtaining an indexed list:
    (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101