easy +10 pts

Union of Two Lists

Return the distinct elements combined from two lists.

Write a function `union_lists(list1, list2)` that takes two lists of integers and returns a new list containing the union of both lists. The union should contain each distinct element exactly once, preserving the order of first occurrence across both lists (elements from `list1` first, then elements from `list2` that are not already in the result). The order of the result must follow this rule. Do not modify the input lists. ### Function signature: ```python def union_lists(list1, list2): pass ```

Constraints

- Input lists may be empty. - Elements are integers, but the logic works for any hashable type. - The result should be a list. - Length of each list is up to 1000.

Example

```python
>>> union_lists([1, 2, 3], [3, 4, 5])
[1, 2, 3, 4, 5]
>>> union_lists([], [1, 1, 2])
[1, 2]
>>> union_lists([1, 1], [2, 2])
[1, 2]
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a set to track seen elements.
Iterate over list1 first and add unique elements to result.
Then iterate over list2 and add only elements not in the set.
The result order is determined by first occurrence.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.