Open In App

Python – Change the signs of elements of tuples in a list

Improve
Improve
Like Article
Like
Save
Share
Report

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




# initializing lists
test_list = [(3, -1), (-4, -3), (1, 3), (-2, 5), (-4, 2), (-9, -3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = []
for sub in test_list:
 
    # 2nd element converted to negative magnitude
    res.append((abs(sub[0]), -abs(sub[1])))
 
# printing result
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




# initializing lists
test_list = [(3, -1), (-4, -3), (1, 3), (-2, 5), (-4, 2), (-9, -3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# list comprehension used as one liner
res = [(abs(sub[0]), -abs(sub[1])) for sub in test_list]
 
# printing result
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.

  1. Define the original list of tuples
  2. Use the map() function with a lambda function to apply the desired transformation to each tuple in the original list
  3. Create a new list with the updated tuples
  4. 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
Previous
Next
Share your thoughts in the comments
Similar Reads