- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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)
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]]
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.
- Python – Filter rows with required elements
- Python – Filter rows with Elements as Multiple of K
- Python – Filter Sorted Rows
- Filter the rows – Python Pandas
- Python – Filter tuple with all same elements
- Python Program to Filter Rows with a specific Pair Sum
- Python – Filter Strings within ASCII range
- How to find specific array elements in MongoDB document with query and filter with range?
- Python – Rows with all List elements
- Python – Filter rows without Space Strings
- Python – Extract tuples with elements in Range
- Python – Filter rows with only Alphabets from List of Lists
- 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