Reverse a Tuple

Tags: reverse, tuple

Write a Python program to reverse a tuple.

Example 1:

Input: (1, 2, 3, 4, 5) 
Output: (5, 4, 3, 2, 1)

Example 2:

Input: ('a', 'b', 'c', 'd', 'e') 
Output: ('e', 'd', 'c', 'b', 'a')

Use the reversed() function for this task, and then convert the result back to a tuple.

def reverse_tuple(t):
    return tuple(reversed(t))

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!