chevron_left
Documentation
Constructor floatConstructor setMethod dirMethod hasattrMethod helpMethod printMethod zipMethod mapMethod enumerateMethod reversedConstructor sliceMethod octMethod typeMethod ordMethod chrConstructor boolMethod asciiMethod anyMethod hexMethod binConstructor strConstructor intConstructor tupleMethod roundMethod nextConstructor rangeMethod inputMethod reprMethod maxMethod minMethod isinstanceConstructor listMethod divmodMethod sumMethod idMethod sortedMethod allMethod lenMethod abs
check_circle
Mark as learned thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings
Python | reversed method
Built-in Functions
chevron_rightDocumentation
schedule Jul 1, 2022
Last updated local_offer Python
Tags tocTable of Contents
expand_more Check out the interactive map of data science
Python's reversed(~)
method returns a reverse iterator from the input sequence.
Parameters
1. seq
| sequence
The sequence to return the reverse iterator for.
Return value
Returns a reverse iterator from the input sequence.
Examples
Basic usage
To return a reverse iterator and create a list based off of it:
list(reversed('Hello'))
['o', 'l', 'l', 'e', 'H']
reversed('Hello')
returns a reverse iterator which we in turn pass as input to list(~)
to generate a list from it.
To return a reverse iterator and print each item:
a = ['Apple', 'Pineapple', 'Mango', 'Pear']reverse_iterator = reversed(a)for element in reverse_iterator: print(element)
PearMangoPineappleApple
We return a reverse iterator from reversed(a)
and store it to reverse_iterator
. We then proceed to loop through each element in reverse_iterator
and print it. Notice that we print the elements in the original list a
in reverse order.
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
Official Python Documentation
https://docs.python.org/3/library/functions.html#reversed
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!