Python next()

Configurare noua (How To)

Situatie

The next() function returns the next item from the iterator.

Solutie

Pasi de urmat

random = [5, 9, ‘cat’]

# converting the list to an iterator
random_iterator = iter(random)
print(random_iterator)

# Output: 5
print(next(random_iterator))

# Output: 9
print(next(random_iterator))

# Output: ‘cat’
print(next(random_iterator))

# This will raise Error
# iterator is exhausted
print(next(random_iterator))

Output:

<list_iterator object at 0x7feb49032b00>
5
9
cat
Traceback (most recent call last):
File “python”, line 18, in <module>
StopIteration

Tip solutie

Permanent

Voteaza

(12 din 18 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?