Python Reverse a Tuple
To reverse a Tuple in Python, call reversed() builtin function and pass the tuple object as argument to it. reversed() returns a reversed object. Pass this reversed object as argument to tuple() function. Reversed tuple is ready.
Examples
In the following program, we take a tuple x
, and reverse it using reversed() and tuple() functions.
Python Program
x = (2, 4, 6) result = reversed(x) result = tuple(result) print(result)
Output
(6, 4, 2)
Now, let us take a tuple of strings, and reverse the tuple.
Python Program
x = ('apple', 'banana', 'cherry') result = reversed(x) result = tuple(result) print(result)
Output
('cherry', 'banana', 'apple')
Conclusion
In this Python Tutorial, we learned how to reverse a Tuple in Python, with examples.