Python - Extract tuples with elements in Range
Last Updated :
26 Apr, 2023
Given list of tuples, extract tuples having elements in range.
Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 5, 6 Output : [(5, 6)] Explanation : Only 1 tuple lies in range of 5-6. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 1, 10 Output : [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)] Explanation : All lie in specified range.
Method #1 : Using list comprehension + all()
In this, we check for all elements in range using comparison operator and all() returns true for tuples that contain tuples in range specified.
Python3
# Python3 code to demonstrate working of
# Extract tuples with elements in Range
# Using all() + list comprehension
# initializing list
test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
strt, end = 4, 7
# list comprehension to encapsulate in 1 liner
# all() checks for all elements in range
res = [sub for sub in test_list if all(ele >= strt and ele <= end for ele in sub)]
# printing results
print("Filtered tuples : " + str(res))
OutputThe original list is : [(4, 5, 7), (5, 6), (3, 8, 10), (4, 10)]
Filtered tuples : [(4, 5, 7), (5, 6)]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #2 : Using filter() + lambda + all()
In this, we employ filter() to extract out tuples, according to function provided by lambda, and all() as utility to test for all elements in range tuple.
Python3
# Python3 code to demonstrate working of
# Extract tuples with elements in Range
# Using filter() + lambda + all()
# initializing list
test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
strt, end = 4, 7
# list() to get back result as list
# all() checks for all elements in range
res = list(filter(lambda sub : all(ele >= strt and ele <= end for ele in sub), test_list))
# printing results
print("Filtered tuples : " + str(res))
OutputThe original list is : [(4, 5, 7), (5, 6), (3, 8, 10), (4, 10)]
Filtered tuples : [(4, 5, 7), (5, 6)]
Method 3 : using a for loop
- Initialize a list of tuples test_list with some sample data.
- Define the range of values we want to extract from the tuples as strt and end.
- Create an empty list res to store the filtered tuples.
- Use a for loop to iterate through each tuple in test_list.
- Within the for loop, use the all() function to check if all elements in the current tuple are within the given range.
- If all elements are within the range, append the tuple to res.
- After the loop has finished iterating through all tuples, print the filtered tuples in res.
Python3
# initializing list
test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)]
# initializing range
strt, end = 4, 7
# initializing an empty list to store the filtered tuples
res = []
# iterating through each tuple in the list
for tup in test_list:
# checking if all elements of the tuple are within the range
if all(strt <= ele <= end for ele in tup):
# if yes, adding the tuple to the result list
res.append(tup)
# printing results
print("Filtered tuples : " + str(res))
OutputFiltered tuples : [(4, 5, 7), (5, 6)]
The time complexity of this approach is O(n*m), where n is the number of tuples in the list and m is the number of elements in each tuple.
The space complexity is O(k), where k is the number of tuples that satisfy the condition.
Similar Reads
Python - Element Index in Range Tuples Sometimes, while working with Python data, we can have a problem in which we need to find the element position in continuous equi ranged tuples in list. This problem has applications in many domains including day-day programming and competitive programming. Let's discuss certain ways in which this t
7 min read
Python - Extract Elements from Ranges in List We are given a list and list containing tuples we need to extract element from ranges in tuples list. For example, n = [10, 20, 30, 40, 50, 60, 70, 80, 90] and r = [(1, 3), (5, 7)] (ranges) we need to extract elements so that output should be [[20, 30, 40], [60, 70, 80]].Using List ComprehensionList
3 min read
Python - Extract Tuples with all Numeric Strings Given a list of tuples, extract only those tuples which have all numeric strings. Input : test_list = [("45", "86"), ("Gfg", "1"), ("98", "10")] Output : [('45', '86'), ('98', '10')] Explanation : Only number representing tuples are filtered. Input : test_list = [("Gfg", "1")] Output : [] Explanatio
5 min read
Python - Filter Rows with Range Elements Given a Matrix, filter all the rows which contain all elements in the given number range. Input : test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]], i, j = 2, 5 Output : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]] Explanation : 2, 3, 4, 5 all are present in above rows. Input :
5 min read
Python - Extract tuples having K digit elements Given a list of tuples, extract all tuples having K digit elements. Input : test_list = [(54, 2), (34, 55), (222, 23), (12, 45), (78, )], K = 2 Output : [(34, 55), (12, 45), (78,)] Explanation : All tuples have numbers with 2 digits. Input : test_list = [(54, 2), (34, 55), (222, 23), (12, 45), (782,
6 min read