Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python reversed() Function



ThePython reversed() function returns an iterator object which allows us to access the specified sequence in reverse order. This function is commonly used in the situation where we want to iterate over the elements of a sequence in reverse, without modifying the original sequence.

Note that thereversed() function only works with those sequence or collections that supports indexing, such aslists,tuples, andstrings. Since sets and dictionaries are unordered collections, this function does not support them. Thereversed() is one of thebuilt-in functions and you do not need to import any module to use this.

Syntax

The syntax of the Pythonreversed() function is as follows −

reversed(seqObj)

Parameters

The Pythonreversed() function accepts a single parameter −

  • seqObj − It represents an sequence object such as a list, string, or tuple.

Return Value

The Pythonreversed() function returns an iterator object.

reversed() Function Examples

Practice the following examples to understand the use ofreversed() function in Python:

Example: Use of reversed() Function

The following example shows the usage of Python reversed() function on a built-in sequence object. Here, we are creating and reversing a list.

numericList = [12, 14, 18, 20, 10, 5]revIterator = reversed(numericList)# Converting reverse iterator to listrevList = list(revIterator)print("Displaying list in reverse order:")print(revList)

When we run above program, it produces following result −

Displaying list in reverse order:[5, 10, 20, 18, 14, 12]

Example: Reverse Characters of String Using reversed()

Since the string is also a sequence object, the reversed() can be apply to it. If we reverse a string, this function will return its characters in reverse order as shown in the below Python code.

orgnlStr = "TutorialsPoint"print("Before Reversing:", orgnlStr)reversedStr = ''.join(reversed(orgnlStr))print("After Reversing:", reversedStr)

Following is an output of the above code −

Before Reversing: TutorialsPointAfter Reversing: tnioPslairotuT

Example: reversed() Function With Loop to Iterate List

The code below demonstrates how to use the reversed() function with thefor loop to iterate and print the items of sequence objects in reverse order.

numericList = [12, 14, 18, 20, 10, 5]print("Displaying list in reverse order:")for revList in reversed(numericList):   print(revList)

Output of the above code is as follows −

Displaying list in reverse order:51020181412

Example: reversed() Function With Range Object

In the code below a range object is defined usingrange() function. Then we reverse it with the help of reversed() function. We are able to reverse this object because it is an immutable sequence type.

rangeObject = range(11, 20, 2)print("Displaying range object item in reverse order:")for revList in reversed(rangeObject):   print(revList)

Following is an output of the above code −

Displaying range object item in reverse order:1917151311
python_built_in_functions.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp