Get Only Positive Values in Python List Tutorial Example

04-May-2023

.

Admin

Get Only Positive Values in Python List Tutorial Example

Hi Dev,

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

There are several ways to get positive 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 positive values

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

print(newList)

Output:

[1, 2, 3, 4, 5, 6, 7]

#Python