0 out of 464 challenges solved
Write a function `group_tuples` that takes a list of tuples as input and groups the tuples by their first element. The function should return a list of tuples where each tuple contains the first element followed by all the unique second elements grouped together. #### Example Usage ```python [main.nopy] print(group_tuples([('x', 'y'), ('x', 'z'), ('w', 't')])) # Output: [('x', 'y', 'z'), ('w', 't')] print(group_tuples([('a', 'b'), ('a', 'c'), ('d', 'e')])) # Output: [('a', 'b', 'c'), ('d', 'e')] print(group_tuples([('f', 'g'), ('f', 'g'), ('h', 'i')])) # Output: [('f', 'g'), ('h', 'i')] ``` #### Constraints - The input list will contain tuples of at least two elements. - The first element of each tuple will be a string. - The second elements will also be strings. - The order of the grouped tuples in the output should follow the order of their first appearance in the input list.
def group_tuples(input_list): """ Groups tuples by their first element and combines the second elements. Args: input_list (list): A list of tuples to be grouped. Returns: list: A list of grouped tuples. """ # Initialize a dictionary to store grouped elements grouped = {} # Iterate through the input list for item in input_list: # Placeholder for processing logic pass # Convert the dictionary to a list of tuples result = [] return result