Python | Reverse sign of each element in given list
Last Updated :
24 Mar, 2023
Given a list of integers, write a Python program to reverse the sign of each element in given list.
Examples:
Input : [-1, 2, 3, -4, 5, -6, -7]
Output : [1, -2, -3, 4, -5, 6, 7]
Input : [-5, 9, -23, -2, 7]
Output : [5, -9, 23, 2, -7]
Methods #1: List comprehension
Python3
# Python3 program to Convert positive
# list integers to negative and vice-versa
def Convert(lst):
return [ -i for i in lst ]
# Driver code
lst = [-1, 2, 3, -4, 5, -6, -7]
print(Convert(lst))
Output:[1, -2, -3, 4, -5, 6, 7]
Time Complexity: O(n).
Space Complexity: O(1).
Methods #2 : Using numpy
The Python module, Numpy, can also be used which is the most pythonic way to solve the given problem. The list is first converted to numpy array and then, the negative of the array is returned, which is finally converted to list.
Python3
# Python3 program to Convert positive
# list integers to negative and vice-versa
import numpy as np
def Convert(lst):
lst = np.array(lst)
return list(-lst)
# Driver code
lst = [-1, 2, 3, -4, 5, -6, -7]
print(Convert(lst))
Output:[1, -2, -3, 4, -5, 6, 7]
Time Complexity: O(n).
Space Complexity: O(1).
Method #3 : Using startswith() and replace() methods
Python3
# Python3 program to Convert positive
# list integers to negative and vice-versa
lst = [-1, 2, 3, -4, 5, -6, -7]
nl = list(map(str, lst))
res = []
for i in nl:
if(i.startswith("-")):
i = i.replace("-", "")
res.append(int(i))
else:
i = "-"+i
res.append(int(i))
print(res)
Output[1, -2, -3, 4, -5, 6, 7]
Time Complexity: O(n).
Space Complexity: O(n).
Method #4: Using map function and neg operator methods
Python
# Python3 program to Convert positive
# list integers to negative and vice-versa
from operator import neg
lst = [-1, 2, 3, -4, 5, -6, -7]
nl = list(map(neg, lst))
print(nl)
Output:
[1, -2, -3, 4, -5, 6, 7]
Time Complexity: O(n).
Space Complexity: O(1).
Method #5 : Using find(),int()+slicing
Approach
Convert integer list to string list using list(),map(),str
- Initiate a for loop to traverse string list
- For each string if there is - sign at the beginning slice string 1 to end , convert it to integer and append to output list
- If not append - sign at the beginning, convert to integer and append to output list
- Display output list
Python3
# Python3 program to Convert positive
# list integers to negative and vice-versa
lst = [-1, 2, 3, -4, 5, -6, -7]
nl = list(map(str, lst))
res = []
for i in nl:
if(i.find("-")==0):
res.append(int(i[1:]))
else:
i = "-"+i
res.append(int(i))
print(res)
Output[1, -2, -3, 4, -5, 6, 7]
Time Complexity : O(N) N - number of elements in list
Auxiliary Space : O(N) N - number of elements in list
Similar Reads
Reverse All Strings in String List in Python We are given a list of strings and our task is to reverse each string in the list while keeping the order of the list itself unchanged. For example, if we have a list like this: ['gfg', 'is', 'best'] then the output will be ['gfg', 'si', 'tseb'].Using For LoopWe can use a for loop to iterate over th
2 min read
Python | Reverse each tuple in a list of tuples 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 : Nega
5 min read
Python - K elements Reversed Slice Given List of elements, perform K elements reverse slice. Input : test_list = [2, 4, 6, 8, 3, 9, 12, 15, 16, 18], K = 3 Output : [18, 16, 15] Explanation : 3 elements sliced from rear end. Input : test_list = [2, 4, 6, 8], K = 3 Output : [8, 6, 4] Explanation : 3 elements sliced from rear end, 8, 6
4 min read
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)
3 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
Remove Negative Elements in List-Python The task of removing negative elements from a list in Python involves filtering out all values that are less than zero, leaving only non-negative numbers. Given a list of integers, the goal is to iterate through the elements, check for positivity and construct a new list containing only positive num
3 min read
Python - Reverse Slicing of given string Reverse slicing in Python is a way to access string elements in reverse order using negative steps.Using Slicing ([::-1])Using slicing with [::-1] in Python, we can reverse a string or list. This technique works by specifying a step of -1, which starts at the end of the sequence and moves backward,
1 min read
Python - Negative index of Element in List We are given a list we need to find the negative index of that element. For example, we are having a list li = [10, 20, 30, 40, 50] and the given element is 30 we need to fin the negative index of it so that given output should be -3.Using index()index() method in Python searches for the first occur
3 min read
Reverse a List in Python Without Using reverse() Function While reverse() is the easiest and most direct method to reverse a list in Python it's useful to know alternative approaches to reverse a list without relying on this function. Using SlicingWe can use Pythonâs list-slicing feature to create a reversed list version without modifying the original one.
2 min read
Reverse sequence of strictly increasing integers in a list-Python The task of reversing the sequence of strictly increasing integers in a list in Python involves identifying consecutive increasing subsequences and reversing each subsequence individually. For example, given a list a = [0, 1, 9, 8, 7, 5, 3, 14], the goal is to reverse the strictly increasing subsequ
3 min read