Python list iterator behavior and next(iterator)

后端 未结 6 999
不知归路
不知归路 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:30

    What is happening is that next(a) returns the next value of a, which is printed to the console because it is not affected.

    What you can do is affect a variable with this value:

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

提交回复
热议问题