Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem in using enumerate
#1
hi
in below code:
name=["ali", "mohammad","fatemeh"]
age=[9,8,12]
fields=["engineering","pilot","doctor"]
result=list(zip(name,age,fields))
e=enumerate(result)
for index , data in e:
      print("%s), '    ',%s"%(index,data))
# some data will be shown.
for index , data in e:
      print("%s), '    ',%s"%(index,data))
# but nothing is displayed, why?
for i in list(e):
      print(i)
# but again nothing is displayed.
list[e]
# the [] is displayed. why?
I ran the above lines in idle.
when you write and run the first for loop(lines 6.7), some data will be shown to you. but then if you write and run the for loop again (lines 9,10), nothing will be displayed to you, also as you can see in the code, list(e) is empty. why?
thanks
Reply
#2
(Oct-09-2023, 01:12 PM)akbarza Wrote: when you write and run the first for loop(lines 6.7), some data will be shown to you. but then if you write and run the for loop again (lines 9,10), nothing will be displayed to you, also as you can see in the code, list(e) is empty. why?
thanks

Not "some data" but "all data".

enumerate() creates enumerate object which in turn uses iterator's __next()__ method. So iterator gets exhausted:

Quote:A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.

So:

>>> e = enumerate(range(10, 13))
>>> print(*e)
(0, 10) (1, 11) (2, 12)
>>> print(*e)

>>> e = enumerate(range(10, 13))
>>> e = list(enumerate(range(10, 13)))  # make list from enumerate object for reuse
>>> print(*e)
(0, 10) (1, 11) (2, 12)
>>> print(*e)
(0, 10) (1, 11) (2, 12)
akbarza likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
The first for loop uses up all the values in the iterable "e". There is nothing left to use. If you try to get another item, a StopIteration exception is raised.
e = enumerate("abcd")
for index, letter in e:
    print(index, letter)
next(e)
Output:
0 a 1 b 2 c 3 d Traceback (most recent call last): File "...", line 4, in <module> next(e) StopIteration
A for loop does the next(iterable) and try/except StopIteration for you. Kind of like this:
e = enumerate("abcd")
while True:
    try:
        index, letter = next(e)
        print(index, letter)
    except StopIteration:
        break
akbarza likes this post
Reply
#4
(Oct-09-2023, 03:36 PM)perfringo Wrote:
(Oct-09-2023, 01:12 PM)akbarza Wrote: when you write and run the first for loop(lines 6.7), some data will be shown to you. but then if you write and run the for loop again (lines 9,10), nothing will be displayed to you, also as you can see in the code, list(e) is empty. why?
thanks

Not "some data" but "all data".

enumerate() creates enumerate object which in turn uses iterator's __next()__ method. So iterator gets exhausted:

Quote:A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.

So:

>>> e = enumerate(range(10, 13))
>>> print(*e)
(0, 10) (1, 11) (2, 12)
>>> print(*e)

>>> e = enumerate(range(10, 13))
>>> e = list(enumerate(range(10, 13)))  # make list from enumerate object for reuse
>>> print(*e)
(0, 10) (1, 11) (2, 12)
>>> print(*e)
(0, 10) (1, 11) (2, 12)

hi
I have not seen * before in such syntax. what is it doing and for what is it used?
thanks
Reply
#5
(Oct-11-2023, 09:52 AM)akbarza Wrote: I have not seen * before in such syntax. what is it doing and for what is it used?

This is iterable unpacking as described in PEP448. It is convenient and concise way in interactive interpretator to output iterable content.
akbarza likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The use of enumerate Frankduc 3 1,702 Jan-31-2022, 09:40 PM
Last Post: deanhystad
  Need to understand the way enumerate() function works abhishekambastha 2 2,491 Sep-16-2018, 08:21 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020