![]() |
VOOZH | about |
We are given a tuple and our task is to reverse whole tuple. For example, tuple t = (1, 2, 3, 4, 5) so after reversing the tuple the resultant output should be (5, 4, 3, 2, 1).
Most common and Pythonic way to reverse a tuple is by using slicing with a step of -1, here is how we can do it:
(5, 4, 3, 2, 1)
Explanation:
reversed() function returns an iterator that can be converted to a tuple.
(5, 4, 3, 2, 1)
Explanation:
We can manually reverse the tuple by iterating from the end using a loop.
(5, 4, 3, 2, 1)
Explanation:
In this method we are using collections.deque, which provides a reverse() method for in-place reversal of the tuple.
(5, 4, 3, 2, 1)
Explanation: