Why does list () with an object show different results respectively?

Result None

the list(a)

second time. Does anyone know about this?

>>> test = {1: 2, 3: 4}
>>> a= test.iterkeys()
>>> list(a)
**[1, 3]**
>>> list(a)
**[]**
>>> list(a)
[]

      

+3


source to share


1 answer


iterkeys

returns an iterator that, like any iterator, can only be iterated once.



list

consumes the entire iterator, so the latter cannot supply more values, so subsequent lists are empty.

+7


source







All Articles