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?
5 Respuestas
+ 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'))
+ 1
Just curious: is there a way to access a memory location directly?
0
do this:-
a = enumerate([i for i in range(1, 10)])
print(list(a))
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.