Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<enumerate object at 0x7f0211ea2360>

I ran the following code in Python3.5.2 and got the corresponding output

>>> example = ['a','b','c','d','e']
>>> enumerate(example)
<enumerate object at 0x7f0211ea2360>

I'm unable to understand what is the meaning of this output.Why didn't the output come like this

 (0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e') 

When i used a list to contain these tuples the output was satisfactory

>>> list(enumerate(example))
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]

Note : I'm a newbie in python and when i posted this question i didn't know about map function so i didn't refer this question Why does map return a map object instead of a list in Python 3?

like image 807
Masquerade Avatar asked Feb 06 '23 08:02

Masquerade


1 Answers

That's purely a choice of design in Python 3 because enumerate is often used in loops/list comprehension, so no need to generate a full-fledged list and allocate memory for a temporary object which is very likely to be unused afterwards.

Most people use for i,e in enumerate(example): so they don't even notice the generator aspect, and the memory/CPU footprint is lower.

To get an actual list or set, you have to explicitly force the iteration like you did.

(note that as opposed to range, zip or map, enumerate has always been a generator, even in python 2.7, good choice from the start)

like image 72
Jean-François Fabre Avatar answered Feb 08 '23 14:02

Jean-François Fabre