VOOZH about

URL: https://www.geeksforgeeks.org/python/python-filter-consecutive-elements-tuples/

⇱ Python - Filter consecutive elements Tuples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.


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.


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


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.

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.

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.

Comment