Reverse the Tuple


Python - Reverse Tuple

To reverse the sequence in tuple object, call reversed() builtin function and pass the tuple as argument.

reversed() function with tuple as argument, returns an object to type reversed. Pass this reversed type object to tuple() builtin function and this function returns a tuple object. So, finally we are getting a new tuple object with the reversed sequence of the original tuple object we passed to reversed() function.

Examples

1. Reverse a Tuple using reversed() built-in function

In this example, we will

  1. Take a tuple object initialized with some values.
  2. Pass tuple object to reversed() builtin function. Store the result in reversed_tuple.
  3. Pass reversed_tuple to tuple() builtin function. Store the result in result.

Python Program

myTuple = ('a', 'b', 'c')
reversed_tuple = reversed(myTuple)
result = tuple(reversed_tuple)
print(result)

Output

('c', 'b', 'a')

Summary

In this tutorial of Python Examples, we learned how to reverse a Tuple sequence in Python using reversed() builtin function.