0 out of 464 challenges solved
Write a Python function `swap_first_last(lst)` that takes a list as input and returns a new list where the first and last elements are interchanged. If the list has fewer than two elements, return the list as is. #### Example Usage ```python [main.nopy] swap_first_last([12, 35, 9, 56, 24]) # Output: [24, 35, 9, 56, 12] swap_first_last([1, 2, 3]) # Output: [3, 2, 1] swap_first_last([4]) # Output: [4] swap_first_last([]) # Output: [] ```
def swap_first_last(lst): """ Swap the first and last elements of the list. Args: lst (list): The input list. Returns: list: A new list with the first and last elements swapped. """ # Check if the list has fewer than two elements if len(lst) < 2: return lst # Placeholder for swapping logic pass