- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 – Extract tuples with elements in Range
When it is required to extract tuples with elements in a given range, the filter and lambda methods are used.
Example
Below is a demonstration of the same −
my_list = [(13, 15, 17), (25, 56), (13, 21, 19 ), (44, 14)] print("The list is :") print(my_list) beg, end = 13, 22 my_result = list(filter(lambda sub : all(element >= beg and element <= end for element in sub), my_list)) print("The result is :") print(my_result)
Output
The list is : [(13, 15, 17), (25, 56), (13, 21, 19), (44, 14)] The result is : [(13, 15, 17), (13, 21, 19)]
Explanation
A list of tuple is defined and is displayed on the console.
The values for beginning and end are defined and are displayed on the console.
A lambda method is used along with ‘all’ operator, to check if an element is greater than beginning value, and less than the end value.
If yes, it is filtered out using ‘filter’ method and converted to a list.
This result is assigned to a variable
This is the output that is displayed on the console.
- Related Articles
- Extract tuples having K digit elements in Python
- Python – Extract elements in between multiple specific range of index
- Python program to find Tuples with positive elements in List of tuples
- Python – Extract range of Consecutive similar elements ranges from string list
- Python program to find Tuples with positive elements in a List of tuples
- Python – Filter Rows with Range Elements
- Python – Extract elements with equal frequency as value
- Python – Filter consecutive elements Tuples
- Find Dissimilar Elements in Tuples in Python
- Trim tuples by N elements in Python
- Extract tuples with specified common values in another column in MySQL?
- Python program to extract rows with common difference elements
- Delete elements in range in Python
- Python – Extract elements from Ranges in List
- Initialize tuples with parameters in Python

Advertisements