0 out of 464 challenges solved
Write a Python function `min_k` that takes a list of tuples and an integer `K` as input. Each tuple contains a string and an integer. The function should return the `K` tuples with the smallest integer values, sorted in ascending order of these values. #### Example Usage ```python [main.nopy] # Example 1 input_list = [('Manjeet', 10), ('Akshat', 4), ('Akash', 2), ('Nikhil', 8)] K = 2 print(min_k(input_list, K)) # Output: [('Akash', 2), ('Akshat', 4)] # Example 2 input_list = [('Sanjeev', 11), ('Angat', 5), ('Akash', 3), ('Nepin', 9)] K = 3 print(min_k(input_list, K)) # Output: [('Akash', 3), ('Angat', 5), ('Nepin', 9)] # Example 3 input_list = [('tanmay', 14), ('Amer', 11), ('Ayesha', 9), ('SKD', 16)] K = 1 print(min_k(input_list, K)) # Output: [('Ayesha', 9)] ``` #### Constraints - The input list will contain at least `K` tuples. - The integer values in the tuples are unique. - The function should return a new list and not modify the input list.
def min_k(test_list, K): """ Find the minimum K records from a list of tuples based on the second element of each tuple. Args: test_list (list): A list of tuples where each tuple contains a string and an integer. K (int): The number of minimum records to return. Returns: list: A list of K tuples with the smallest integer values. """ # Sort the list based on the second element of the tuples # Return the first K elements of the sorted list pass