Open In App

Python | Reverse each tuple in a list of tuples

Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of tuples, write a Python program to reverse each tuple in the given list of tuples. 

Examples:

Input : [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
Output : [(2, 1), (5, 4, 3), (9, 8, 7, 6)]

Input : [('a', 'b'), ('x', 'y'), ('m', 'n')]
Output : [('b', 'a'), ('y', 'x'), ('n', 'm')]

  Method #1 : Negative-step slicing We can use standard negative-step slicing tup[::-1] to get the reverse of a tuple, and a list comprehension to get that for each tuple. 

Python3




# Python3 program to Reverse
# each tuple in a list of tuples
 
def reverseTuple(lstOfTuple):
     
    return [tup[::-1] for tup in lstOfTuple]
             
# Driver code
lstOfTuple = [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
print(reverseTuple(lstOfTuple))


Output

[(2, 1), (5, 4, 3), (9, 8, 7, 6)]

  Method #2 : Using reversed() The in-built reversed() method of Python can also be used to reverse each tuple within list. 

Python3




# Python3 program to Reverse
# each tuple in a list of tuples
 
def reverseTuple(lstOfTuple):
     
    return [tuple(reversed(tup)) for tup in lstOfTuple]
             
# Driver code
lstOfTuple = [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
print(reverseTuple(lstOfTuple))


Output

[(2, 1), (5, 4, 3), (9, 8, 7, 6)]

  Method #3 : Using map() function Python map() function can also serve the purpose by mapping negative-step slicing to list of tuples. 

Python3




# Python3 program to Reverse
# each tuple in a list of tuples
 
def reverseTuple(lstOfTuple):
     
    return list(map(lambda tup: tup[::-1], lstOfTuple))
             
# Driver code
lstOfTuple = [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
print(reverseTuple(lstOfTuple))


Output

[(2, 1), (5, 4, 3), (9, 8, 7, 6)]

  Method #3 :Using a list comprehension and zip

We can use a list comprehension and the zip function to reverse each tuple. The zip function takes multiple iterables as arguments and returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the input iterables. By passing a tuple and its reversed version to zip, we can create a new tuple with the elements of the original tuple in reverse order.

Python3




def reverseTuple(lstOfTuple):
    return [tup[0] for tup in zip(tup[::-1] for tup in lstOfTuple)]
# Driver code
lstOfTuple = [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
print(reverseTuple(lstOfTuple))
#This code is contributed by Edula Vinay Kumar Reddy


Output

[(2, 1), (5, 4, 3), (9, 8, 7, 6)]

Time complexity: O(n) when n length is all the elements in all tuples

Auxiliary Space: O(n)

 Using a list comprehension and tuple unpacking:

In this approach, we will use a list comprehension to iterate through the list of tuples and reverse each tuple using tuple unpacking.

Python3




# Example list of tuples
tuples_list = [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
 
# Reversing each tuple in the list using a list comprehension
tuples_list = [tuple(reversed(t)) for t in tuples_list]
 
print(tuples_list)


Output

[(2, 1), (5, 4, 3), (9, 8, 7, 6)]

Time complexity: O(n*m) where n is the number of tuples in the list and m is the maximum length of a tuple.
Auxiliary Space: O(n)

 Using Recursive method.

Algorithm:

1. Define a function that takes a list of tuples as input
2. Check if the input list is empty, if so return an empty list
3. If the list is not empty, recursively call the function on the input list excluding the last tuple and concatenate the result with the last tuple reversed.
4. Return the result obtained in step 3.

Python3




def reverseTuple(lstOfTuple):
    if not lstOfTuple:
        return []
    else:
        return reverseTuple(lstOfTuple[:-1])+[lstOfTuple[-1][::-1]]
 
 
# Driver code
lstOfTuple = [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
print(reverseTuple(lstOfTuple))
#This code is contributed by tvsk


Output

[(2, 1), (5, 4, 3), (9, 8, 7, 6)]

The time complexity of this implementation is O(n^2) due to the use of concatenation operator in each recursive call. 

The space complexity is also O(n^2) due to the creation of new tuples in each recursive call.

Using numpy:

Here’s a step-by-step breakdown of the algorithm:

The function is defined with the name reverseTuple and a parameter lstOfTuple which represents the input list of tuples.
Inside the function, a list comprehension is used to create a new list of tuples.
For each tuple tup in the input list lstOfTuple, the np.flip() function from the NumPy library is used to reverse the order of the elements in the tuple. The resulting reversed tuple is then converted to a tuple using the tuple() function.
The list comprehension returns a new list of reversed tuples, which is returned by the function.
Finally, the function is called with an example list of tuples [(1, 2), (3, 4, 5), (6, 7, 8, 9)] and the resulting list of reversed tuples is printed using the print() function.

Python3




import numpy as np
 
def reverseTuple(lstOfTuple):
    return [tuple(np.flip(tup)) for tup in lstOfTuple]
 
# Driver code
lstOfTuple = [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
print(reverseTuple(lstOfTuple))


Output:
[(2, 1), (5, 4, 3), (9, 8, 7, 6)]

Time complexity: O(n*m) where n is the number of tuples in the list and m is the maximum length of a tuple. This is because the np.flip() function takes linear time to reverse each tuple, and we need to do this for each tuple in the list.

Auxiliary space: O(n*m) because we create a new list of tuples with the reversed elements.



Last Updated : 09 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads