Python reversed() FunctionPython Functions

Difficulty Level:
Difficulty Level:

The reversed() function returns an iterator object of the given sequence in the reverse order.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

# string
seq = 'ABCDEF'
print(seq)
print(list(reversed(seq)))

# tuple
seqTuple = ('M', 'I', 'T')
print(seqTuple)
print(list(reversed(seqTuple)))

Output:

ABCDEF
['F', 'E', 'D', 'C', 'B', 'A']
('M', 'I', 'T')
['T', 'I', 'M']

Syntax

reversed(sequence)

Parameters

Name Description
sequence Any iterable object

Return Value

It returns a reversed iterator object.

Advertisement

© 2021 Pythonexample.org, all rights reserved. Privacy Policy | Contact Us