ItSolutionStuff.com

How to Get Only Positive Values in Python List?

By Hardik Savani • October 30, 2023
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: 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 Elements Greater Than Some Value Example

Read Now →

Python Convert List to String with Space Example

Read Now →

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

Read Now →

Python Remove First 2, 3, 4, 5, 10, etc Elements from List Example

Read Now →

Python List Replace Double Quotes with Single Example

Read Now →

Python Read Text File into List Example

Read Now →

Python Generate Text File from List Example

Read Now →

How to Reverse List Elements in Python?

Read Now →

How to Add Element at Specific Index in Python List?

Read Now →

How to Count Number of Elements in a List in Python?

Read Now →

How to Get Min Value from Python List?

Read Now →

Python Split String into List of Characters Example

Read Now →