Masquerade
Masquerade

Reputation: 3890

<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?

Upvotes: 5

Views: 13510

Answers (3)

Noor
Noor

Reputation: 11

Understanding enumerate

Say train_ids is a list of my training object.

aa=enumerate(train_ids)
type(aa)
for i in aa:
    print(i)

Upvotes: 1

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140266

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)

Upvotes: 11

viki
viki

Reputation: 1

l1 = ['a','b','c','d','e']
for i in enumerate(l1):
   print i

Upvotes: -3

Related Questions