CKTIS / Python
reversed()
Vytvorí spätný iterátor pomocou `__reversed__` alebo dĺžky a indexovania sekvencie.
Verzia: Python 3.14; repository image, production runtime not attested
Syntax
reversed(sequence, /)Vytvorí spätný iterátor pomocou __reversed__ alebo dĺžky a indexovania sekvencie.
Vydá 8, 4 a 2, kým zoznam si ponechá 2, 4, 8. Druhý prechod tým istým iterátorom je prázdny.
Jednosmerný iterátor nemusí obrátenie podporovať.
a = [2, 4, 8]
it = reversed(a)
assert list(it) == [8, 4, 2] and a == [2, 4, 8] and list(it) == []
Príklady
items = [2, 4, 8]
it = reversed(items)
print(list(it))
print(items, list(it))
Výsledok
[8, 4, 2] [2, 4, 8] []
Jednosmerný iterátor nemusí obrátenie podporovať.