ItSolutionStuff.com

How to Get Only Negative Values in Python List?

By Hardik Savani • October 30, 2023
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: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Python List Get All Items Less Than Some Value Example

Read Now →

Python List Get All Elements Greater Than Some Value Example

Read Now →

Python Convert List to String with Space Example

Read Now →

How to Get Last n Elements of a List in Python?

Read Now →

How to Get Last 2, 3, 4, 5, 10, etc Elements from List in Python?

Read Now →

Python List Replace Double Quotes with Single Example

Read Now →

How to Add Element to a List in Python?

Read Now →

Python List Print All Elements Except First Example

Read Now →

How to Convert List to Lowercase in Python?

Read Now →

Python Split String into List of Characters Example

Read Now →

How to Remove Duplicate Values from List in Python?

Read Now →

How to Remove null Values from the List in Python?

Read Now →

Python Get Second Last Element of List Example

Read Now →

How to Remove Last Element from List in Python?

Read Now →