VOOZH about

URL: https://www.geeksforgeeks.org/python/python-remove-consecutive-tuple-according-to-key/

⇱ Python | Remove Consecutive tuple according to key - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python | Remove Consecutive tuple according to key

Last Updated : 12 Apr, 2023

Sometimes, while working with Python list, we can have a problem in which we can have a list of tuples and we wish to remove them basis of first element of tuple to avoid it's consecutive duplication. Let's discuss certain way in which this problem can be solved. 

Method : Using groupby() + itemgetter() + next() This task can be performed using combination of these functions. In this, we convert the list to iterator for faster access using next(), itemgetter() is used to get the index of tuple on which we need to perform the deletion(in this case first) and groupby() performs the final grouping of elements. 

Output : 
The original list is : [(4, 5), (4, 6), (7, 8), (7, 1), (7, 0), (8, 1)]
List after Consecutive tuple removal : [(4, 5), (7, 8), (8, 1)]

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method#2 : Using loop()

In this code, we loop over each tuple in the original list and check if its first element is different than the previous tuple's first element (or if it's the first tuple). If it is, we add it to the filtered list. If it's not, we skip it. This way, we only keep the first tuple with each unique first element in the original list. Finally, we print the filtered list.


Output
[(4, 5), (7, 8), (8, 1)]

Time complexity: O(n)
Auxiliary Space: O(k)

Method#3 : Using numpy:

 Algorithm:

  1. Create the original list of tuples: original_list = [(4, 5), (4, 6), (7, 8), (7, 1), (7, 0), (8, 1)]
  2. Convert the original list into a numpy array: array = np.array(original_list)
  3. Use array[:, 0] to get the first column of the numpy array, which contains the unique values we want to filter by.
  4. Use np.unique with return_index=True to get the indices of the unique elements in the first column: indices = np.unique(array[:, 0], return_index=True)[1]
  5. Use these indices to extract the unique rows from the original array: unique_rows = array[indices]
  6. Convert the unique rows back into tuples using a list comprehension: filtered_list = [tuple(row) for row in unique_rows]
  7. Print the resulting filtered list: print(filtered_list)

Output:

The original list is : [(4, 5), (4, 6), (7, 8), (7, 1), (7, 0), (8, 1)]
List after Consecutive tuple removal : [(4, 5), (7, 8), (8, 1)]
 

Time complexity:

Converting the original list to a numpy array takes O(n) time, where n is the number of elements in the list.
Finding the unique elements based on the first column using np.unique() takes O(nlogn) time.
Extracting the unique elements based on the indices takes O(k), where k is the number of unique elements.
Converting the extracted elements back to tuples takes O(k) time.
Therefore, the overall time complexity is O(nlogn), dominated by the np.unique() function.

Space complexity:

Converting the original list to a numpy array takes O(n) space.
Finding the unique elements based on the first column using np.unique() takes O(n) space for the intermediate arrays created.
Extracting the unique elements based on the indices takes O(k) space for the output array.
Converting the extracted elements back to tuples takes O(k) space for the output list.
Therefore, the overall space complexity is O(n).

Comment
Article Tags: