Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why list() with an object shows different results respectively? [duplicate]

Tags:

python

list

The outcome is None with list(a) the second time. Anyone have a clue on that?

>>> test = {1: 2, 3: 4}
>>> a= test.iterkeys()
>>> list(a)
**[1, 3]**
>>> list(a)
**[]**
>>> list(a)
[]
like image 852
I am Learning... Avatar asked Jan 18 '26 18:01

I am Learning...


1 Answers

iterkeys returns an iterator, which, as any iterator, can be iterated over only once.

list consumes the whole iterator, so that the latter can't provide any more values, so the subsequent lists are empty.

like image 126
ForceBru Avatar answered Jan 21 '26 07:01

ForceBru