Python | Reverse each tuple in a list of tuples
Last Updated :
09 May, 2023
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
def reverseTuple(lstOfTuple):
return [tup[:: - 1 ] for tup in lstOfTuple]
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
def reverseTuple(lstOfTuple):
return [ tuple ( reversed (tup)) for tup in lstOfTuple]
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
def reverseTuple(lstOfTuple):
return list ( map ( lambda tup: tup[:: - 1 ], lstOfTuple))
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)]
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) 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
tuples_list = [( 1 , 2 ), ( 3 , 4 , 5 ), ( 6 , 7 , 8 , 9 )]
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 ]]
lstOfTuple = [( 1 , 2 ), ( 3 , 4 , 5 ), ( 6 , 7 , 8 , 9 )]
print (reverseTuple(lstOfTuple))
|
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]
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.
Similar Reads
How To Slice A List Of Tuples In Python?
In Python, slicing a list of tuples allows you to extract specific subsets of data efficiently. Tuples, being immutable, offer a stable structure. Use slicing notation to select ranges or steps within the list of tuples. This technique is particularly handy when dealing with datasets or organizing i
3 min read
Swap tuple elements in list of tuples - Python
The task of swapping tuple elements in a list of tuples in Python involves exchanging the positions of elements within each tuple while maintaining the list structure. Given a list of tuples, the goal is to swap the first and second elements in every tuple. For example, with a = [(3, 4), (6, 5), (7,
3 min read
Python - Flatten tuple of List to tuple
The task of flattening a tuple of lists to a tuple in Python involves extracting and combining elements from multiple lists within a tuple into a single flattened tuple. For example, given tup = ([5, 6], [6, 7, 8, 9], [3]), the goal is to flatten it into (5, 6, 6, 7, 8, 9, 3). Using itertools.chain(
3 min read
Generating a "Set Of Tuples" from A "List of Tuples" - Python
We are given a list of tuples and we need to extract only the unique tuples while removing any duplicates. This is useful in scenarios where you want to work with distinct elements from the list. For example:We are given this a list of tuples as [(1, 2), (3, 4), (1, 2), (5, 6)] then the output will
3 min read
Python | Combining tuples in list of tuples
Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways
7 min read
How To Reverse A List In Python Using Slicing
In Python, list slicing is a common practice and it is the most used technique for programmers to solve efficient problems. In this article, we will see how we can reverse a list using slicing in Python. Reverse a List Using Slicing in PythonBelow are some of the examples by which we can perform rev
3 min read
How to Take List of Tuples as Input in Python?
Lists of tuples are useful data structures in Python and commonly used when you need to group related elements together while maintaining immutability within each group. We may need to take input for a list of tuples from the user. This article will explore different methods to take a list of tuples
3 min read
Sort Tuple of Lists in Python
The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [
3 min read
Python - Sort Tuple List by Nth Element of Tuple
We are given list of tuple we need to sort tuple by Nth element of each tuple. For example d = [(1, 5), (3, 2), (2, 8), (4, 1)] and k=1 we need to sort by 1st element of each tuple so that output for given list should be [(4, 1), (3, 2), (1, 5), (2, 8)] Using sorted() with lambdasorted() function wi
3 min read
Slicing List of Tuples in Python
Slicing, a powerful feature in Python, allows us to extract specific portions of data from sequences like lists. When it comes to lists of tuples, slicing becomes a handy tool for extracting and manipulating data. In this article, we'll explore five simple and versatile methods to slice lists of tup
2 min read