A Python union of lists usually means combining two or more lists and keeping each value once. The best method depends on whether you care about order. A set union is concise and removes duplicates, but it does not preserve the original list order. An order-preserving approach is better when the output should follow the first appearance of each item.
Python lists do not have a built-in union() method. The union() method belongs to sets, not lists. To union lists, you either convert to sets, use a dictionary for ordered deduplication, or loop through the values explicitly. The Python tutorial on sets is useful background if you want the mathematical version of union.
Union of Two Lists With set()
If order does not matter, convert both lists to sets and combine them. This is the shortest method for hashable values such as strings, integers, and tuples. A set-style union removes duplicates; How to Combine Lists in Python covers concatenation and other combinations when order and repeated values must be preserved.
list_a = [1, 2, 3]
list_b = [3, 4, 5]
union = list(set(list_a) | set(list_b))
print(union)
The output contains unique values from both lists, but the order may vary. That is acceptable for membership-style work, but not for reports or user-facing output where order matters. For a separate guide on the conversion step, see convert list to set in Python.
Preserve Order With dict.fromkeys()
For many list-union tasks, preserving order is the right default. Concatenate the lists, then use dict.fromkeys() to keep the first occurrence of each value.
list_a = ["red", "blue", "green"]
list_b = ["green", "yellow", "red"]
union = list(dict.fromkeys(list_a + list_b))
print(union)
This returns values in the order they first appear: items from the first list first, followed by new items from the second list. It works because dictionary keys are unique and keep insertion order in modern Python. This is often the best practical answer when someone asks for the union of two lists.
Union of More Than Two Lists
When you have three or more lists, avoid chaining many + operations manually. The itertools.chain() function can flatten several lists into one stream before deduplication.
from itertools import chain
first = [1, 2, 3]
second = [3, 4]
third = [4, 5, 6]
union = list(dict.fromkeys(chain(first, second, third)))
print(union)
This keeps the first occurrence of each value across all lists. It also scales better when the number of lists is not fixed, because you can pass a collection of lists to chain.from_iterable(). That makes it useful for data collected from several files, API responses, or batches.
Use a Loop for Custom Rules
A loop is useful when you need extra conditions, such as ignoring empty strings, converting values, or keeping only values that pass a validation rule.
list_a = ["Python", "", "NumPy"]
list_b = ["python", "Pandas", ""]
seen = set()
union = []
for value in list_a + list_b:
key = value.strip().lower()
if key and key not in seen:
seen.add(key)
union.append(value.strip())
print(union)
This version deduplicates case-insensitively while preserving a readable display value. It is more verbose, but it makes the business rule explicit. For list-state checks in similar code, see check if a list is empty in Python.
Keep Duplicates When You Mean Concatenation
Sometimes people say “union” when they actually mean “append all items from both lists.” If duplicates should remain, use concatenation instead of union logic.
list_a = [1, 2, 3]
list_b = [3, 4, 5]
combined = list_a + list_b
print(combined)
This produces [1, 2, 3, 3, 4, 5]. That is not a mathematical union because the duplicate 3 remains. It is still correct when you are joining event logs, rows, or repeated values intentionally. The key is to decide whether duplicates carry meaning before choosing the method.
Union Lists That Contain Unhashable Items
Set-based methods require hashable items. If your lists contain dictionaries or other unhashable objects, choose a key that represents uniqueness and track that key in a set.
left = [{"id": 1, "name": "Ada"}, {"id": 2, "name": "Lin"}]
right = [{"id": 2, "name": "Lin"}, {"id": 3, "name": "Max"}]
seen = set()
union = []
for item in left + right:
if item["id"] not in seen:
seen.add(item["id"])
union.append(item)
print(union)
This keeps one dictionary for each unique id. The same idea works for objects, tuples with selected fields, and rows loaded from files. If you later need to arrange compound records, the guide to sorting a list of tuples in Python is a useful follow-up.
Common Mistakes
The biggest mistake is calling list.union(). Lists do not provide that method. Another mistake is using set union when the output order matters. Also remember that set-based union only works with hashable values. If the list contains dictionaries, lists, or mutable objects, use a key-based loop instead.
For very large lists, think about memory. Concatenating lists creates another list, while iterating with chain() can avoid some intermediate copying. For background on how Python lists grow, see Python dynamic arrays. For small lists, readability usually matters more than micro-optimizing the union operation.
Conclusion
Use list(set(a) | set(b)) when order does not matter and all values are hashable. Use list(dict.fromkeys(a + b)) when you want a clean, order-preserving union of lists. Use a loop when values need normalization, filtering, or key-based deduplication. The right method depends on whether order, duplicates, and item type matter in your data.