How to Get Only Positive Values in Python List?

By Hardik Savani October 30, 2023 Category : Python

Hi Dev,

In this short guide, we will show you how to get positive items from python list. I am going to show you about how to get only integer value from list in python. If you have a question about how to make positive numbers from python list then I will give a simple example with a solution. We will use how to take only positive values in python list. you will do the following things for how to get only positive values in python list.

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

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:

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

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:

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

I hope it can help you...

Tags :
Shares