Python List Get All Elements Greater Than Some Value Example

By Hardik Savani October 30, 2023 Category : Python

Hi Artisan,

In this quick guide, we will teach you python list get all elements greater than some value. you'll learn python list get values greater than certain value. I explained simply about python all items in list greater than other. Here you will learn get items greater than value in python list.

There are several ways to get all items greater than certain value. In this example, we will get all values that greater than "7" value. we will use filter and sorted() to get elements that greater than other value. so let's see the below 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 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  
# Python list get all elements greater than some value
newList = [item for item in myList if item > 7]
  
print(newList)

Output:

[8, 9, 10, 11, 12]

Example 2:

main.py

# Create New List with Item
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  
# Python list get all elements greater than some value
newList = sorted(item for item in myList if item > 7)
  
print(newList)

Output:

[8, 9, 10, 11, 12]

I hope it can help you...

Tags :
Shares