
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Filter Rows with Range Elements
When it is required to filter rows with range elements, a list comprehension and the ‘all’ operator is used to determine the output.
Below is a demonstration of the same −
Example
my_list = [[3, 2, 4, 5, 10], [32, 12, 4, 51, 10],[12, 53, 11], [2, 3, 31, 5, 8, 7]] print("The list is :") print(my_list) i, j = 2, 5 my_result = [index for index in my_list if all(element in index for element in range(i, j + 1))] print("The result is :") print(my_result)
Output
The list is : [[3, 2, 4, 5, 10], [32, 12, 4, 51, 10], [12, 53, 11], [2, 3, 31, 5, 8, 7]] The result is : [[3, 2, 4, 5, 10]]
Explanation
A list of list is defined and displayed on the console.
The value for integers ‘i’ and ‘j’ are defined.
A list comprehension is used to iterate over the list, and check if all the elements belong to the range specified by the two integers previously defined.
If yes, it is converted to a list.
This result is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Python – Filter rows with required elements
- Python – Filter rows with Elements as Multiple of K
- Python – Filter Sorted Rows
- Python – Filter tuple with all same elements
- Filter the rows – Python Pandas
- Python – Filter Strings within ASCII range
- Python – Rows with all List elements
- Python Program to Filter Rows with a specific Pair Sum
- Python – Filter rows with only Alphabets from List of Lists
- Python – Filter rows without Space Strings
- How to find specific array elements in MongoDB document with query and filter with range?
- Python – Extract tuples with elements in Range
- Python – Filter consecutive elements Tuples
- Python - Filter Rows Based on Column Values with query function in Pandas?
- Python program to extract rows with common difference elements

Advertisements