Python Program to Reverse Tuple

Write a Python Program to reverse the Tuple items, rearranging its elements from last to first. There are multiple ways to reverse the tuple times, and the most prominent way is using the built-in reversed() function or the slicing technique. However, you can use the for loop or any other iterator to achieve the result.

Python Program to Reverse Tuple Items using slicing

We used the Tuple slice with a negative step value in this example to reverse the numeric, string, mixed, and nested Tuples. Here, ‘[::-1]’ means no start and stop value (read all values) but the negative step value. It instructs the Python to start from the last item and move one step backward until it reaches the first item.

intRTuple = (10, 30, 19, 70, 40, 60)
print("Original Tuple Items = ", intRTuple)

revIntTuple = intRTuple[::-1]
print("Tuple Items after Reversing = ", revIntTuple)

strRTuple = ('apple', 'Mango', 'kiwi')
print("String Tuple Items = ", strRTuple)

revStrTuple = strRTuple[::-1]
print("String Tuple after Reversing = ", revStrTuple)

mixRTuple = ('Apple', 22, 'Kiwi', 45.6, (1, 3, 7), 16, [1, 2])
print("Mixed Tuple Items = ", mixRTuple)

revMixTuple = mixRTuple[::-1]
print("Mixed Tuple after Reversing = ", revMixTuple)
Python Program to Reverse integer and string Tuple

Using reversed() function

We used the reversed function in this Python program to reverse the Tuple. The reversed function (reversed(intRTuple)) returns the reversed object, so we must convert it back to Tuple.

intRTuple = (3, 78, 44, 67, 34, 11, 19)
print("Original Tuple Items = ", intRTuple)

revTuple = reversed(intRTuple)
print("Data Type = ", type(revTuple))

revIntTuple = tuple(revTuple)
print("Tuple Items after Reversing = ", revIntTuple)
print("Tuple Data Type = ", type(revIntTuple))
Original Tuple Items =  (3, 78, 44, 67, 34, 11, 19)
Data Type =  <class 'reversed'>
Tuple Items after Reversing =  (19, 11, 34, 67, 44, 78, 3)
Tuple Data Type =  <class 'tuple'>

Python Program to Reverse Tuple using the For loop

In this example, the for loop with reversed function iterates the tuple item from last to first. Within the loop, we are adding each tuple item to revIntTuple. Remember, tuples are immutable, so they won’t change the original tuple; instead, they create a new one.

intRTuple = (10, 19, 29, 39, 55, 60, 90, 180)
print("Original = ", intRTuple)

revintTuple = ()

for i in reversed(range(len(intRTuple))):
    revintTuple += (intRTuple[i],)

print("After = ", revintTuple)
Original =  (10, 19, 29, 39, 55, 60, 90, 180)
After =  (180, 90, 60, 55, 39, 29, 19, 10)

Here, we used for loop range (for i in range(len(intRTuple) – 1, 0, -1)) to iterate tuple items from last to first and add them to reverse Tuple.

intRTuple = (10, 19, 29, 39, 55, 60, 90, 180)
print("Original Tuple Items = ", intRTuple)

revintTuple = ()

for i in range(len(intRTuple) - 1, 0, -1):
    revintTuple += (intRTuple[i],)

print("After Reversing the Tuple = ", revintTuple)
Python Program to Reverse a Tuple 4