pythonintermediate10 minutes

Build a Function to Merge and Sort Unique Elements from Two Lists

Create a Python function that takes two lists of integers and returns a sorted list containing all unique elements from both lists.

Challenge prompt

Write a function named merge_and_sort_unique that accepts two lists of integers as input parameters. The function should merge the two lists, remove any duplicate values, and return a new list of the unique elements sorted in ascending order.

Guidance

  • Combine both lists into one before processing.
  • Remove duplicates efficiently without using nested loops.
  • Sort the resulting list before returning.

Hints

  • Consider using set operations to remove duplicates quickly.
  • The built-in sorted() function can be used to sort the list.
  • List concatenation can be done with the + operator.

Starter code

def merge_and_sort_unique(list1, list2):
    # Your code here
    pass

Expected output

merge_and_sort_unique([4, 1, 3], [3, 6, 2]) # Output: [1, 2, 3, 4, 6]

Core concepts

list manipulationsetssorting

Challenge a Friend

Send this duel to someone else and see if they can solve it.