Code Logo

Combined Supply List with set()

Published at22 Apr 2026
Python Data Structures Easy 0 views
Like0

Two classroom teams are preparing supplies for the same workshop. Each team wrote its own list of needed items, and some names appear on both lists. The teacher now wants one clean checklist without repeated item names.

Your task is to combine the two lists and return a new list containing each item only once. To make the final checklist easy to read, the returned list should be sorted in alphabetical order.

For example, if the lists are ["glue", "paper", "tape"] and ["paper", "markers", "glue"], the result should be ["glue", "markers", "paper", "tape"]. Even though "glue" and "paper" appear in both inputs, they should appear only once in the final answer.

If both input lists are empty, the answer should be an empty list. This challenge is about merging collections, removing duplicates, and producing a clean final result.

Example Input & Output

Example 1
Input
team_a = ["paint"], team_b = ["brush", "paint"]
Output
["brush", "paint"]
Explanation

A repeated item stays only once in the sorted result.

Example 2
Input
team_a = ["glue", "paper", "tape"], team_b = ["paper", "markers", "glue"]
Output
["glue", "markers", "paper", "tape"]
Explanation

Items from both lists are merged, duplicates are removed, and the result is sorted.

Example 3
Input
team_a = [], team_b = []
Output
[]
Explanation

Two empty lists produce an empty combined checklist.

Algorithm Flow

Recommendation Algorithm Flow for Combined Supply List with set()
Recommendation Algorithm Flow for Combined Supply List with set()

Solution Approach

This problem combines two separate ideas that fit together nicely. First, you need to remove duplicates. Second, you need the final answer in alphabetical order. Python's set is very helpful for the first part because sets only keep unique values.

A clean way to solve it is to convert both lists into sets, combine them, and then sort the result:

return sorted(set(team_a) | set(team_b))

The operator | performs a union. That means it keeps every distinct item that appears in either input collection. If an item appears in both lists, it still ends up only once in the union set.

After that, sorted() turns the unique items into a list and places them in alphabetical order. That last step matters because sets do not preserve a predictable display order, while the problem explicitly asks for a sorted list.

You could also write the union with the method form set(team_a).union(team_b). That is equally correct. The important data-structure idea is that sets are ideal when the problem cares about unique membership rather than repeated occurrences.

This approach stays concise without hiding the logic. Build a unique collection first, then arrange it in the output format the problem wants. The runtime is roughly O(n + m + k log k), where k is the number of distinct items being sorted.

Best Answers

python - Approach 1
def combine_supplies(team_a, team_b):
    return sorted(set(team_a) | set(team_b))