Merging and Sorting Two Lists Function

  • Share this:

Code introduction


This function takes two lists as arguments, merges them, and returns a sorted list. If the inputs are not lists, it will raise a ValueError.


Technology Stack : list, sorted, error handling (try-except)

Code Type : Function

Code Difficulty : Intermediate


                
                    
def sorted_list(arg1, arg2):
    try:
        if not isinstance(arg1, list) or not isinstance(arg2, list):
            raise ValueError("Both arguments must be lists.")
        combined_list = arg1 + arg2
        sorted_list = sorted(combined_list)
        return sorted_list
    except ValueError as e:
        return str(e)