How to Get Only Negative Values in Python List?

By Hardik Savani October 30, 2023 Category : Python

Hey Friends,

Here, I will show you how to work how to get negative items from python list. If you have a question about how to get only negative value from list in python then I will give a simple example with a solution. I would like to share with you how to make negative numbers from python list. If you have a question about how to take only negative values in python list then I will give a simple example with a solution.

In this examples, i will take on myList list variable with positive and negative values. Then i will use filter and sorted() function to get only negative value from python list. so let's see the following example.

You can use these examples with python3 (Python 3) version.

let's see below a simple example with output:

Example 1:

main.py

# Create New List with Item
myList = [-3, -2, -1, 1, 2, 3, 4, 5, 6, 7]
    
# Python list get positive values
newList = [item for item in myList if item < 0]	
    
print(newList)

Output:

[-3, -2, -1]

Example 2:

main.py

# Create New List with Item
myList = [-3, -2, -1, 1, 2, 3, 4, 5, 6, 7]
  
# Python list get positive values
newList = sorted(item for item in myList if item < 0)
  
print(newList)

Output:

[-3, -2, -1]

I hope it can help you...

Tags :
Shares