How To Reverse A Tuple In Python

Reverse a tuple in Python

A Python tuple is an immutable data type. You cannot change its value after creating it. Fortunately, Python provides several functions that make it possible for us to reverse a tuple in Python. We will discuss those functions in this article.

How to reverse a tuple in Python?

You can reverse a tuple in three different ways.

Using reversed() method

The reversed() method returns an iterator that traverses the iterable in reverse order. It takes an iterable object as an argument and returns an iterator that produces the items from the iterable object in reverse order.

Syntax:

reversed(iterable)

Parameters:

  • iterable: an iterable object you want to reverse.

To reverse a tuple, call the reversed() method. This method will return an iterator that goes through the tuple elements in reverse order.

You can iterate this iterator to get the elements in reverse order, put them in a new list, and convert the list to a tuple using tuple(). Like this:

myTuple = ('IT', 'Share', 'Learn')
print('My tuple:', myTuple)

# Use reversed() to get an iterator in reverse order
reversedIt = reversed(myTuple)
print(reversedIt)

# Put the elements of the reversed object into a new list
reversedList = []

for item in reversedIt:
    reversedList.append(item)

# Convert the list to tuple
reversedTuple = tuple(reversedList)
print('Reversed tuple:', reversedTuple)

Output:

My tuple: ('IT', 'Share', 'Learn')
<reversed object at 0x0000018F93CF2230>
Reversed tuple: ('Learn', 'Share', 'IT')

Using tuple slicing

Slicing is a process of extracting a specific part from a tuple. It is done by using the index number. The first value is always the start index, and the second is always the end index.

Syntax:

<Name of a tuple>[start:end:step]

Parameters:

  • start: the index mark where the slicing begins.
  • end: the index mark where the slicing ends.
  • step: allows you to select items within the range from start to end.

We can also reverse a tuple using the following syntax: [::- 1]

This slicing syntax means start at position 0, end before position 0, and move from the end of the tuple to the start.

myTuple = ('IT', 'Share', 'Learn')
print('My tuple:', myTuple)

# Reverse myTuple using tuple slicing
reversedTuple = myTuple[::-1]
print('Reversed tuple:', reversedTuple)

Output:

My tuple: ('IT', 'Share', 'Learn')
Reversed tuple: ('IT', 'Learn')

Using indexing

To reverse a tuple in Python, we first iterate over it in reverse order by accessing its [-i - 1] element. In each iteration, we will perform a concatenation of the two tuples. Like this:

myTuple = ('IT', 'Share', 'Learn')
print('My tuple:', myTuple)

# Create a new tuple and join the elements together in reverse order
reversedTuple = ()

for i in range(len(myTuple)):
    # Use [-i - 1] to loop in reverse order and use += to join 2 tuples
    reversedTuple += (myTuple[-i - 1],)

print('Reversed tuple:', reversedTuple)

Output:

My tuple: ('IT', 'Share', 'Learn')
Reversed tuple: ('Learn', 'Share', 'IT')

Summary

We’ve introduced three ways to show you how to reverse a tuple in Python. The reversed() method is the fastest method for performing the reverse operation on a tuple. We hope you will remember it and be able to use it when you need to reverse a tuple.

Have a beautiful day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *