0 out of 464 challenges solved
Write a function `count_list_occurrences` that takes a list of lists as input and returns a dictionary. Each key in the dictionary should be a tuple representation of a sublist, and the corresponding value should be the count of how many times that sublist appears in the input list. #### Example Usage ```python [main.nopy] count_list_occurrences([[1, 2], [3, 4], [1, 2], [5, 6]]) # Output: {(1, 2): 2, (3, 4): 1, (5, 6): 1} count_list_occurrences([['a', 'b'], ['c'], ['a', 'b'], ['a', 'b']]) # Output: {('a', 'b'): 3, ('c',): 1} ```
def count_list_occurrences(list_of_lists): """ Count the occurrences of each sublist in the input list of lists. Args: list_of_lists (list): A list containing sublists. Returns: dict: A dictionary where keys are tuples representing sublists and values are their counts. """ # Initialize an empty dictionary to store the counts result = {} # Iterate through the list of lists for sublist in list_of_lists: # Convert the sublist to a tuple (tuples are hashable and can be dictionary keys) sublist_tuple = tuple(sublist) # Update the count in the dictionary result[sublist_tuple] = result.get(sublist_tuple, 0) + 1 # Return the resulting dictionary return result