YouTip LogoYouTip

Python Func Next

# Python2.x Python next() Function [![Image 3: Python Built-in Functions](#) Python Built-in Functions](#) * * * ## Description **next()** returns the next item from an iterator. The **next()** function should be used in conjunction with the **iter()** function that creates an iterator. ## Syntax next syntax: next(iterable[, default]) Parameter description: * iterable -- An iterable object * default -- Optional. This is the value to be returned if the iterator is exhausted (no next element). If not provided and the iterator is exhausted, a StopIteration exception is raised. ## Return Value Returns the next item. ## Example The following example demonstrates the usage of next: #!/usr/bin/python# -*- coding: UTF-8 -*-# First obtain the Iterator object:it = iter([1, 2, 3, 4, 5])# Loop:while True: try: # Get the next value:x = next(it)print(x)except StopIteration: # Exit the loop when StopIteration is encountered break The output is: 12345 [![Image 4: Python Built-in Functions](#) Python Built-in Functions](#)
← Python Func DirPython Func Complex β†’