Python list iterator behavior and next(iterator)

后端 未结 6 981
不知归路
不知归路 2020-12-04 07:45

Consider:

>>> lst = iter([1,2,3])
>>> next(lst)
1
>>> next(lst)
2

So, advancing the iterator is, as expected, ha

6条回答
  •  有刺的猬
    2020-12-04 08:48

    Something is wrong with your Python/Computer.

    a = iter(list(range(10)))
    for i in a:
       print(i)
       next(a)
    
    >>> 
    0
    2
    4
    6
    8
    

    Works like expected.

    Tested in Python 2.7 and in Python 3+ . Works properly in both

提交回复
热议问题