Convert all positive numbers in a List to negative in Python
source link: https://thispointer.com/convert-all-positive-numbers-in-a-list-to-negative-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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.
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:
[-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.
Pandas Tutorials -Learn Data Analysis with Python
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.
Join a LinkedIn Community of Python Developers
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK