Efficient way to union two list with list or None value
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Working with lists in Python often requires the task of combining or "unioning" them to remove duplicates and ensure that only unique elements are maintained. The challenge becomes more nuanced when dealing with lists that might contain `None` values. This article will explore an efficient way to union two lists, considering cases where lists might contain `None`.
Understanding the Problem
When we talk about unioning two lists in Python, we're generally looking to combine the elements of both lists into one, with unique values. Here's what makes this task a bit tricky:
- Handling `None` Values: `None` is a unique entity in Python that behaves differently compared to other data types.
- Efficiency: We need to ensure that the method used is efficient, particularly when working with large lists.
Methodology
Let's dive into the technical part. To union two lists while accounting for `None` values, we can make use of the `set` data structure in Python. A set inherently handles unique values, including `None`. Here's a breakdown of how this can be done efficiently:
Using `set` to Union Lists
The `set` data structure is an unordered and unindexed collection of items. In Python, sets are written with curly brackets `{}`.
Steps:
- Convert both lists to sets.
- Use the set union operation (`|`) to merge them.
- Convert the result back to a list if necessary.
Example:
• Convert to Sets: We first check if the list is not `None`, converting it to a set or an empty set if it is `None`. • Union Operation: The union (`|`) operation combines both sets while maintaining unique values. • Conversion Back to List: If a list output is desired, converting the final set back to a list allows for ordered iteration. • Empty Lists: An empty list converts to an empty set, resulting in no additional elements in the union. • None Input: If either list is `None`, it's treated as an empty set for the purpose of the operation.
Related reading
- Efficiently convert edge list to adjacency list using MapReduce
- Efficiently find all connected induced subgraphs
- efficiently find amount of integers in a sorted array
- Efficiently find an integer not in a set of size 40, 400, or 4000
- Efficiently finding duplicates in a list
- Efficiently grouping a list of coordinates points by location in Python
- Efficiently find binary strings with low Hamming distance in large set
- Efficiently finding the largest surrounding square in 2D grid

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.