0 out of 464 challenges solved
Write a Python function `filter_negatives(numbers)` that takes a list of integers as input and returns a new list containing only the negative numbers from the input list. #### Example Usage ```python [main.nopy] print(filter_negatives([-1, 4, 5, -6])) # Output: [-1, -6] print(filter_negatives([-1, -2, 3, 4])) # Output: [-1, -2] print(filter_negatives([-7, -6, 8, 9])) # Output: [-7, -6] ```
def filter_negatives(numbers): """ Filters and returns the negative numbers from the input list. Args: numbers (list): A list of integers. Returns: list: A list containing only the negative integers from the input list. """ # Initialize an empty list to store negative numbers negatives = [] # Iterate through the input list for num in numbers: # Check if the number is negative if num < 0: # Add the negative number to the result list negatives.append(num) # Return the list of negative numbers return negatives