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.
Example 1: Reverse Tuple
In this example, we will
- Take a tuple object initialized with some values.
- Pass tuple object to reversed() builtin function. Store the result in
reversed_tupleto tuple() builtin function. Store the result in
myTuple = ('a', 'b', 'c') reversed_tuple = reversed(myTuple) result = tuple(reversed_tuple) print(result)
('c', 'b', 'a')
In this tutorial of Python Examples, we learned how to reverse a Tuple sequence in Python using reversed() builtin function.