Python – Change the signs of elements of tuples in a list
Given a dual Tuple list, the task is to write a python program to convert second element to negative magnitude of each tuple and first element to positive magnitude of each tuple.
Input : test_list = [(3, -1), (-4, -3), (1, 3), (-2, 5), (-4, 2), (-9, -3)]
Output : [(3, -1), (4, -3), (1, -3), (2, -5), (4, -2), (9, -3)]
Explanation : All the first elements are positive, and 2nd index elements are negative, as desired.
Input : test_list = [(3, -1), (-4, -3), (1, 3), (-2, 5)]
Output : [(3, -1), (4, -3), (1, -3), (2, -5)]
Explanation : All the first elements are positive, and 2nd index elements are negative, as desired.
Method 1 : Using loop and abs()
In this, we iterate using loop and initially convert both to positive magnitude using abs(). The 2nd element is signed “-” and is converted to negative element as desired.
Example:
Python3
test_list = [( 3 , - 1 ), ( - 4 , - 3 ), ( 1 , 3 ), ( - 2 , 5 ), ( - 4 , 2 ), ( - 9 , - 3 )]
print ( "The original list is : " + str (test_list))
res = []
for sub in test_list:
res.append(( abs (sub[ 0 ]), - abs (sub[ 1 ])))
print ( "Updated Tuple list : " + str (res))
|
Output:
The original list is : [(3, -1), (-4, -3), (1, 3), (-2, 5), (-4, 2), (-9, -3)]
Updated Tuple list : [(3, -1), (4, -3), (1, -3), (2, -5), (4, -2), (9, -3)]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using list comprehension
Similar to above method, only difference being list comprehension is used as one liner to perform this task.
Example:
Python3
test_list = [( 3 , - 1 ), ( - 4 , - 3 ), ( 1 , 3 ), ( - 2 , 5 ), ( - 4 , 2 ), ( - 9 , - 3 )]
print ( "The original list is : " + str (test_list))
res = [( abs (sub[ 0 ]), - abs (sub[ 1 ])) for sub in test_list]
print ( "Updated Tuple list : " + str (res))
|
Output:
The original list is : [(3, -1), (-4, -3), (1, 3), (-2, 5), (-4, 2), (-9, -3)]
Updated Tuple list : [(3, -1), (4, -3), (1, -3), (2, -5), (4, -2), (9, -3)]
Time complexity: O(n), where n is the length of the test_list. The list comprehension takes O(n) time to create the final list.
Auxiliary Space: O(1), extra space required is not required
METHOD 3:Using the map function: The given Python code updates the original list of tuples by changing the sign of the second element of each tuple to its opposite and leaving the sign of the first element unchanged. It uses the map() function with a lambda function as an argument to apply the desired transformation to each tuple in the original list, creating a new list with the updated tuples.
- Define the original list of tuples
- Use the map() function with a lambda function to apply the desired transformation to each tuple in the original list
- Create a new list with the updated tuples
- Print the updated list
Python3
original_list = [( 3 , - 1 ), ( - 4 , - 3 ), ( 1 , 3 ), ( - 2 , 5 ), ( - 4 , 2 ), ( - 9 , - 3 )]
updated_list = list (
map ( lambda tup: ( abs (tup[ 0 ]), - 1 * abs (tup[ 1 ])), original_list))
print ( "Updated Tuple List: " , updated_list)
|
Output
Updated Tuple List: [(3, -1), (4, -3), (1, -3), (2, -5), (4, -2), (9, -3)]
Time Complexity: O(n)
Space Complexity: O(n)
Last Updated :
20 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...