near_me
Linear Algebra
keyboard_arrow_down 54 guides
chevron_leftDocumentation
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
Comment auto_stories Bi-column layout
settings
Python | reversed method
schedule Aug 12, 2023
Last updated local_offer
Tags Python
tocTable of Contents
expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
Start your free 7-days trial now!
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
Comment
Citation
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!