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.
Python3
# Python3 code to demonstrate working of
# Remove Consecutive tuple according to key
# using itemgetter() + next() + groupby()
from operator import itemgetter
from itertools import groupby
# initialize list
test_list = [(4, 5), (4, 6), (7, 8), (7, 1), (7, 0), (8, 1)]
# printing original list
print("The original list is : " + str(test_list))
# Remove Consecutive tuple according to key
# using itemgetter() + next() + groupby()
res = [next(group) for key, group in groupby(test_list, key = itemgetter(0))]
# printing result
print("List after Consecutive tuple removal : " + str(res))
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.
Python3
# Define the original list
original_list = [(4, 5), (4, 6), (7, 8), (7, 1), (7, 0), (8, 1)]
# Initialize an empty list to hold the filtered items
filtered_list = []
# Loop over each tuple in the original list
for i in range(len(original_list)):
# Check if this tuple has a different first element than the previous one (or if it's the first one)
if i == 0 or original_list[i][0] != original_list[i-1][0]:
filtered_list.append(original_list[i])
# Print the filtered list
print(filtered_list)
Output[(4, 5), (7, 8), (8, 1)]
Time complexity: O(n)
Auxiliary Space: O(k)
Method#3 : Using numpy:
Algorithm:
- Create the original list of tuples: original_list = [(4, 5), (4, 6), (7, 8), (7, 1), (7, 0), (8, 1)]
- Convert the original list into a numpy array: array = np.array(original_list)
- Use array[:, 0] to get the first column of the numpy array, which contains the unique values we want to filter by.
- 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]
- Use these indices to extract the unique rows from the original array: unique_rows = array[indices]
- Convert the unique rows back into tuples using a list comprehension: filtered_list = [tuple(row) for row in unique_rows]
- Print the resulting filtered list: print(filtered_list)
Python3
import numpy as np
# Define the original list
original_list = [(4, 5), (4, 6), (7, 8), (7, 1), (7, 0), (8, 1)]
# printing original list
print("The original list is : " + str(original_list))
# Convert the list into a numpy array
array = np.array(original_list)
# Get the indices of the unique elements based on the first column
indices = np.unique(array[:, 0], return_index=True)[1]
# Get the unique elements based on the indices
res = [tuple(row) for row in array[indices]]
# printing result
print("List after Consecutive tuple removal : " + str(res))
#This code is contributed by jyothi pinjala.
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).
Similar Reads
Python - Ways to remove a key from dictionary
We are given a dictionary and our task is to remove a specific key from it. For example, if we have the dictionary d = {"a": 1, "b": 2, "c": 3}, then after removing the key "b", the output will be {'a': 1, 'c': 3}.Using pop()pop() method removes a specific key from the dictionary and returns its cor
3 min read
Python - Remove Dictionary Key Words
We are given a dictionary we need to remove the dictionary key words. For example we are given a dictionary d = {'a': 1, 'b': 2} we need to remove the dictionary key words so that the output becomes {'b': 2} . We can use method like del , pop and other methods to do it.Using del keyworddel keyword i
2 min read
Convert Tuple to List in Python
In Python, tuples and lists are commonly used data structures, but they have different properties:Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for furt
2 min read
Python - Remove item from dictionary when key is unknown
We are given a dictionary we need to remove the item or the value of key which is unknown. For example, we are given a dictionary a = {'a': 10, 'b': 20, 'c': 30} we need to remove the key 'b' so that the output dictionary becomes like {'a': 10, 'c': 30} . To do this we can use various method and app
4 min read
How to Return if List is Consecutive within Python Column?
In programming, determining if a list of integers is consecutive means checking if the number appears in sequential order without any gaps. For example [1, 2, 3, 4, 5] is consecutive because each number follows the previous one directly.Understanding Consecutive ListsA consecutive list is a sequence
4 min read
Remove empty tuples from a list - Python
The task of removing empty tuples from a list in Python involves filtering out tuples that contain no elements i.e empty. For example, given a list like [(1, 2), (), (3, 4), (), (5,)], the goal is to remove the empty tuples () and return a new list containing only non-empty tuples: [(1, 2), (3, 4),
3 min read
Python - Remove empty value types in dictionaries list
Sometimes, while working with Python dictionaries, we require to remove all the values that are virtually Null, i.e does not hold any meaningful value and are to be removed before processing data, this can be an empty string, empty list, dictionary, or even 0. This has applications in data preproces
7 min read
Python | Remove consecutive duplicates from list
Removing consecutive duplicates from a list means eliminating repeated elements that appear next to each other in the list. If an element repeats consecutively, only the first occurrence should remain and the duplicates should be removed.Example:Input: ['a', 'a', 'b', 'b', 'c', 'a', 'a', 'a']Output:
3 min read
How to Remove Item from a List in Python
Lists in Python have various built-in methods to remove items such as remove, pop, del and clear methods. Removing elements from a list can be done in various ways depending on whether we want to remove based on the value of the element or index.The simplest way to remove an element from a list by i
3 min read
How to iterate through list of tuples in Python
In Python, a list of tuples is a common data structure used to store paired or grouped data. Iterating through this type of list involves accessing each tuple one by one and sometimes the elements within the tuple. Python provides several efficient and versatile ways to do this. Letâs explore these
2 min read