Get Only Negative Values in Python List Tutorial Example

05-May-2023

.

Admin

Get Only Negative Values in Python List Tutorial Example

Hi Dev,

This article will provide an example of how to get only negative values in a Python list. I’m going to show you Python lists that get only negative values. you'll learn Python lists get only negative values. you can see python all only negative. Alright, let’s dive into the steps.

There are several ways to get negative values from the python list. I will give you two examples using filter and sorted() to get integer values from a list. so, let's see the following examples.

Example :


main.py

# Create New List with Item

myList = [-3, -2, -1, 1, 2, 3, 4, 5, 6, 7]

# Python list get negative values

newList = [item for item in myList if item < 0]

print(newList)

Output:

[-3, -2, -1]

#Python