Python – Filter consecutive elements Tuples
Last Updated :
15 May, 2023
Given a Tuple list, filter tuples that are made from consecutive elements, i.e diff is 1.
Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 4), (6, 4, 6, 3)]
Output : [(3, 4, 5, 6)]
Explanation : Only 1 tuple adheres to condition.
Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6, 4, 6, 3)]
Output : [(3, 4, 5, 6), (1, 2, 3)]
Explanation : Only 2 tuples adhere to condition.
Method #1: Using loop
In this, for each tuple, we call consecutive elements utility which returns True if tuple is consecutive.
Python3
def consec_check(tup):
for idx in range ( len (tup) - 1 ):
if tup[idx + 1 ] ! = tup[idx] + 1 :
return False
return True
test_list = [( 3 , 4 , 5 , 6 ), ( 5 , 6 , 7 , 2 ), ( 1 , 2 , 3 ), ( 6 , 4 , 6 , 3 )]
print ( "The original list is : " + str (test_list))
res = []
for sub in test_list:
if consec_check(sub):
res.append(sub)
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6, 4, 6, 3)]
The filtered tuples : [(3, 4, 5, 6), (1, 2, 3)]
Time Complexity: O(n*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 list comprehension
In this, we perform a similar function as above, just in one-liner shorthand using list comprehension.
Python3
def consec_check(tup):
for idx in range ( len (tup) - 1 ):
if tup[idx + 1 ] ! = tup[idx] + 1 :
return False
return True
test_list = [( 3 , 4 , 5 , 6 ), ( 5 , 6 , 7 , 2 ), ( 1 , 2 , 3 ), ( 6 , 4 , 6 , 3 )]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if consec_check(sub)]
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6, 4, 6, 3)]
The filtered tuples : [(3, 4, 5, 6), (1, 2, 3)]
Method #3 : Using min(),max() and range() methods
Python3
test_list = [( 3 , 4 , 5 , 6 ), ( 5 , 6 , 7 , 2 ), ( 1 , 2 , 3 ), ( 6 , 4 , 6 , 3 )]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
x = list (i)
a = min (x)
b = max (x)
y = list ( range (a,b + 1 ))
if (x = = y):
res.append(i)
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6, 4, 6, 3)]
The filtered tuples : [(3, 4, 5, 6), (1, 2, 3)]
Method #4: Using the set() function.
Here are the steps:
- Initialize an empty list res to store the filtered tuples.
- Loop through each tuple i in the test_list.
- Convert i into a set using the set() function and store it in a variable s.
- Create a set expected using the set() function with the values in the range between the minimum and maximum values of i inclusive.
- If s and expected are equal, append i to the res list.
- Return the res list.
Python3
test_list = [( 3 , 4 , 5 , 6 ), ( 5 , 6 , 7 , 2 ), ( 1 , 2 , 3 ), ( 6 , 4 , 6 , 3 )]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
s = set (i)
expected = set ( range ( min (i), max (i) + 1 ))
if s = = expected:
res.append(i)
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6, 4, 6, 3)]
The filtered tuples : [(3, 4, 5, 6), (1, 2, 3)]
Time complexity: O(n*k), where n is the number of tuples in the input list and k is the length of the longest tuple.
Auxiliary space: O(k) to store the sets s and expected.
Method #5: Using lambda function and filter() function with slicing
Step by step algorithm:
- Define the input list of tuples.
- Use the filter() function along with a lambda function to filter out non-consecutive tuples.
- Inside the lambda function, for each tuple x in the input list, check if it is equal to a tuple of consecutive integers using the range() function.
- If the tuple is consecutive, it is added to a new list res.
- Convert the filtered list of tuples to a list using the list() function.
- Print the filtered list of tuples.
Python3
test_list = [( 3 , 4 , 5 , 6 ), ( 5 , 6 , 7 , 2 ), ( 1 , 2 , 3 ), ( 6 , 4 , 6 , 3 )]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda x: x = = tuple ( range (x[ 0 ], x[ - 1 ] + 1 )), test_list))
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6, 4, 6, 3)]
The filtered tuples : [(3, 4, 5, 6), (1, 2, 3)]
Time Complexity: O(nm), where n is the length of the input list and m is the maximum length of a tuple in the list.
Space Complexity: O(n), where n is the length of the input list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...