VOOZH about

URL: https://www.geeksforgeeks.org/python/python-replace-tuple-according-to-nth-tuple-element/

⇱ Python | Replace tuple according to Nth tuple element - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python | Replace tuple according to Nth tuple element

Last Updated : 3 May, 2023

Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. 

Method #1: Using loop + enumerate() This task can be performed using the combination of loops and enumerate function which can help to access the Nth element and then check and replace when the condition is satisfied. 


Output
The original list is : [('gfg', 1), ('was', 2), ('best', 3)]
The tuple after replacement is : [('gfg', 1), ('is', 2), ('best', 3)]

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #2: Using list comprehension This is the one-liner approach to solve this particular problem. In this, we just iterate the list element and keep matching the matching Nth element of tuple and perform replacement. 


Output
The original list is : [('gfg', 1), ('was', 2), ('best', 3)]
The tuple after replacement is : [('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), as we are creating a new list to store the result.

Method #3 : Using the map() function

This method uses the map() function to check and replace the element that matches the condition.


Output
The original list is : [('gfg', 1), ('was', 2), ('best', 3)]
The tuple after replacement is : [('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(n), as it iterates through the list of tuples once.
Auxiliary space: O(n) as it creates a new list to store the modified tuples, the size of this list is equal to the size of the original list.

Method #4:  Use the filter() function along with a lambda function 

The filter() function is used to remove the tuples from test_list that don't match the value of the Nth element in repl_rec. The resulting map object is converted to a list using the list() function. Then, the repl_rec tuple is added to the list using the concatenation operator +. The resulting list contains the tuples with the replaced value.


Output
The tuple after replacement is : [('gfg', 1), ('best', 3), ('is', 2)]

Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(n), where n is the number of tuples in the list.

Method #5: Using a simple loop.

  • Define a list of tuples test_list containing three tuples.
  • Define a tuple repl_rec to use as a replacement.
  • Create a new list res by iterating over each tuple in test_list.
  • For each tuple x in test_list, use a ternary expression to check if the second element of x is equal to 2. If it is, replace x with repl_rec, otherwise keep x as it is.
  • Print the updated list res containing the original tuples with the specified replacement tuple.

Output
The tuple after replacement is : [('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(n), where n is the number of tuples in the list.

Method #6: Using itertools.starmap().

Step-by-step approach:

  1. Initialize the list of tuples and the replacement tuple.
  2. Use itertools.starmap() method to iterate over each tuple in the original list.
  3. Use lambda function to check if the Nth element of the current tuple is equal to the Nth element of the replacement tuple.
  4. If the elements are equal, return the replacement tuple, else return the current tuple.
  5. Convert the iterator returned by starmap() to a list and store it in res.
  6. Print the updated list of tuples.

Output
The original list is : [('gfg', 1), ('was', 2), ('best', 3)]
The tuple after replacement is : [('gfg', 1), ('is', 2), ('best', 3)]

Time Complexity: O(n), The time complexity of the lambda function and the starmap() method is O(1) as they execute for each tuple in the list. Therefore, the overall time complexity is O(n), where n is the number of tuples in the list.
Auxiliary Space: O(n) as we create a new list to store the updated tuples.

Method #7: Using reduce() function from functools module

  • Import the functools module to use the reduce() function.
  • Define a lambda function to iterate over the original list and compare the Nth element of each tuple with that of the replacement tuple.
  • If they match, append the replacement tuple to the result list.
  • If they don't match, append the original tuple to the result list.
  • Use the reduce() function with the lambda function and the initial value of an empty list to obtain the updated list of tuples.
  • Print the updated list of tuples.

Output
The tuple after replacement is : [('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.

Comment