0

Enumerate a list

Came across this interesting line of code, but do not understand the result: a=enumerate([i for i in range(1,10)]) I understand that [i for i in range(1,10)] is a list comprehension that creates a list of integers 1 through 9. I do not understand what happens when the list is enumerated. I get the result <enumerate at 0x7f958d7e6af8>. I am guessing that is a reference to a location in memory? Is so, how do I access that location in memory? If not, then what the does it mean?

17th Oct 2019, 11:22 PM
GeoK68
GeoK68 - avatar
5 odpowiedzi
18th Oct 2019, 11:37 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
enumerate creates an iterator. The values are created one by one as needed. You can for example loop over such an iterator. To see what exactly is done, you can simply 'unpack' such an iterator and print the output. print(*enumerate('abc'))
18th Oct 2019, 12:29 AM
HonFu
HonFu - avatar
+ 1
Just curious: is there a way to access a memory location directly?
18th Oct 2019, 11:23 PM
GeoK68
GeoK68 - avatar
0
do this:- a = enumerate([i for i in range(1, 10)]) print(list(a))
17th Oct 2019, 11:43 PM
rodwynnejones
rodwynnejones - avatar
0
hope you don't mind me piggy backing on you post but... can anyone explain this: a = enumerate([i for i in range(1, 10)]) # one for x, y in dict(a).items(): print(x,'=>', y) # two print(list(a)) # three print(tuple(a)) Individually, they work as intended, but if i I try to run all three..only one (which even one is first) output correctly.
18th Oct 2019, 12:17 AM
rodwynnejones
rodwynnejones - avatar