In this article, we will discuss different ways to convert all positive numbers in a List to negative numbers in Python.
Table Of Contents
Method 1: Using List Comprehension and if-else
Inside a List comprehension, iterate over all numbers in a list, and for each number check if it is positive or not. If it is positive, then convert it to negative, and store in the new list. For numbers, which are already negative, store them as it is. Thus List comprehension will give us a list of negative numbers. Let’s see an example,
listOfNumbers = [11, -2, -9, -10, -23, 33, 56, -67, -90] # Convert all postive numbers in a list to negative listOfNumbers = [num*-1 if num > 0 else num for num in listOfNumbers] print(listOfNumbers)
Output:
[-11, -2, -9, -10, -23, -33, -56, -67, -90]
Method 2: Using map() function
Pass a lambda function
, and a list
as arguments to the map() function. This lambda function will accept a number as argument, and returns its negative version. If the given number is already negative, then it will return the same number.
Frequently Asked:
The map() function will apply the given lambda function on each element of the list, and store the returned values to a mapped object. We can cast that mapped object to a list. This way we will have a list of negative numbers only. Let’s see an example,
listOfNumbers = [11, -2, -9, -10, -23, 33, 56, -67, -90] # Convert all postive numbers in a list to negative listOfNumbers = list(map(lambda x: x*-1 if x > 0 else x, listOfNumbers)) print(listOfNumbers)
Output:
[-11, -2, -9, -10, -23, -33, -56, -67, -90]
Method 3: Using for-loop
Iterate from 0 to N, where N is the size of the given list. During iteration, for each index position, select the element from list at that index position, and make it negative. This way we will have a list of negative numbers only. Let’s see an example,
listOfNumbers = [11, -2, -9, -10, -23, 33, 56, -67, -90] # Convert all postive numbers in a list to negative for i in range(len(listOfNumbers)): if listOfNumbers[i] > 0: listOfNumbers[i] = -listOfNumbers[i] print(listOfNumbers)
Output:
Latest Python - Video Tutorial
[-11, -2, -9, -10, -23, -33, -56, -67, -90]
Method 4: Using NumPy
Create a NumPy Array from a list. The apply - operator
on the array. It will apply minus operator on each element of the array. But it will inverse the sign of each number in the array. Positives will become nagative, and negatives will become positive. Then we can convert this array back to list. Let’s see an example,
import numpy as np listOfNumbers = [11, -2, -9, -10, -23, 33, 56, -67, -90] # create a NumPy Array from list arr = np.array(listOfNumbers) # Apply minus operator to each element of array arr = -arr # create a list from numpy array listOfNumbers = arr.tolist() print(listOfNumbers)
Output:
[-11, 2, 9, 10, 23, -33, -56, 67, 90]
This is not what exactly we expected. But it is always good to know an extra technique.
Summary
We learned about different ways to convert all positive numbers in a list to negative numbers in Python.
Latest Video Tutorials