Python next()- Funktion
Beispiel
Erstellen Sie einen Iterator und drucken Sie die Elemente einzeln:
mylist = iter(["apple", "banana", "cherry"])
x =
next(mylist)
print(x)
x = next(mylist)
print(x)
x = next(mylist)
print(x)
Definition und Verwendung
Die next()
Funktion gibt das nächste Element in einem Iterator zurück.
Sie können einen Standardrückgabewert hinzufügen, der zurückgegeben wird, wenn das Iterable sein Ende erreicht hat.
Syntax
next(iterable, default)
Parameterwerte
Parameter | Description |
---|---|
iterable | Required. An iterable object. |
default | Optional. An default value to return if the iterable has reached to its end. |
Mehr Beispiele
Beispiel
Gibt einen Standardwert zurück, wenn das Iterable sein Ende erreicht hat:
mylist = iter(["apple", "banana", "cherry"])
x =
next(mylist, "orange")
print(x)
x = next(mylist, "orange")
print(x)
x =
next(mylist, "orange")
print(x)
x = next(mylist, "orange")
print(x)