Home » Reverse a tuple in Python

Reverse a tuple in Python

by Online Tutorials Library

Reverse a tuple in Python

We know that tuples are the data structures present in Python in which elements of different data types can be enclosed in parenthesis.

In this tutorial, we will learn how we can reverse a tuple in Python.

Consider the following examples to understand our objective-

Explanation –

We can observe here that since we have reversed the elements of the tuple, the float value present at the last comes in the first place.

Let’s see one more example-

Explanation –

We can observe here that since we have reversed the elements of the tuple, the string value present at the last comes in the first place.

We will use the following methods to reverse a tuple in Python,

  1. Using Slicing
  2. Using reversed() method
  3. Using Generator in Python
  4. Using the indexing technique

So, let’s get started with the first one:

Using Slicing

The slicing technique used in Python to slice the elements that lie in a specific range.

The following program illustrates how they can be used.

Output:

The original tuple is:  (1, 2, 'Python', 'Java', 23.4, 77, 10)  The reversed tuple is:  (10, 77, 23.4, 'Java', 'Python', 2, 1)  

Explanation-

It’s time to have a look at the explanation of the above program-

  1. In the first step, we have initialized our tuple with different values.
  2. After this, we have displayed our tuple and then used slicing to print the elements of our tuple in reverse order by specifying the step as -1.
  3. Finally, we have displayed the reversed tuple.

In the second program, we will learn how method can be used.

Using reversed() method

Consider the program given below,

Output:

The original tuple is:  (1, 2, 'Python', 'Java', 23.4, 77, 10)  The reversed tuple is:  (10, 77, 23.4, 'Java', 'Python', 2, 1)  

Explanation-

Let’s understand what happened in the above program,

  1. In the first step, we have initialized our tuple with different values.
  2. After this, we have displayed our tuple and then used the reversed() to print the elements of our tuple in reverse order.
  3. Finally, we have displayed the reversed tuple.

In the third program, we will see how generators can be used for the same.

Using Generator in Python

The program given below demonstrates how generators can be used in our Python program.

Output:

The original tuple is:  (1, 2, 'Python', 'Java', 23.4, 77, 10)  The elements of a reversed tuple are:  10  The elements of a reversed tuple are:  77  The elements of a reversed tuple are:  23.4  The elements of a reversed tuple are:  Java  The elements of a reversed tuple are:  Python  The elements of a reversed tuple are:  2  The elements of a reversed tuple are:  1  

Explanation-

It’s time to have a glance at the explanation,

  1. In the first step, we have initialized our tuple with different values.
  2. After this, we have displayed our tuple and then created a function that takes a tuple as its parameter and helps us to obtain the tuple in reversed order using the concept of generators.
  3. Finally, we have displayed the reversed tuple.

Finally, we will see how indexing can help us to meet our objective.

Using the indexing technique

The following program illustrates how it can be done

Output:

The original tuple is:  (1, 2, 'Python', 'Java', 23.4, 77, 10)  The element of a reversed tuple is:  10  The element of a reversed tuple is:  77  The element of a reversed tuple is:  23.4  The element of a reversed tuple is:  Java  The element of a reversed tuple is:  Python  The element of a reversed tuple is:  2  The element of a reversed tuple is:  1  

Explanation-

It’s time to have a look at the explanation of the above program-

  1. In the first step, we have initialized our tuple with different values.
  2. After this, we have displayed our tuple and then created a function that takes the elements of our tuple, uses for loop to traverse the tuple, and then print the elements in reverse order by specifying the index as -1.
  3. Finally, we have displayed the reversed tuple.

Conclusion

In this tutorial, we discussed the different approaches of reversing a tuple in Python.


You may also like