Python List Get All Items Less Than Some Value Example

By Hardik Savani October 30, 2023 Category : Python

Hello Artisan,

In this tutorial, we will go over the demonstration of python list get all elements less than some value. We will use python list get values less than certain value. In this article, we will implement a python all items in list less than other. you'll learn get items less than value in python list. Let's see below example Python find index of element in list less than.

There are several ways to get all items less than certain value. In this example, we will get all values that less than "7" value. we will use filter and sorted() to get elements that less 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 less than some value
newList = [item for item in myList if item < 7]
  
print(newList)

Output:

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

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 less than some value
newList = sorted(item for item in myList if item < 7)
  
print(newList)

Output:

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

I hope it can help you...

Tags :
Shares