Python - Change the signs of elements of tuples in a list
Last Updated :
20 Apr, 2023
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.
- 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)
OutputUpdated Tuple List: [(3, -1), (4, -3), (1, -3), (2, -5), (4, -2), (9, -3)]
Time Complexity: O(n)
Space Complexity: O(n)
Similar Reads
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
Convert list of strings to list of tuples in Python Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con
5 min read
Python Program to get indices of sign change in a list Given List, the task is to write a python program that extracts all the indices from which sign shift occurred. Input : test_list = [7, 6, -3, -4, -7, 8, 3, -6, 7, 8]Output : [1, 4, 6, 7]Explanation : 6 -> -3, at 1st index, -7 -> 8 at 4th index and so on are shifts. Input : test_list = [7, 6,
6 min read
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
Python | Convert list of tuples to list of list Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples.Using numpyNumPy makes it easy to convert a list of tu
3 min read