Python - Append Missing Elements from Other List
Last Updated :
01 Feb, 2025
We are given two lists, and our task is to append elements from the second list to the first list, but only if they are not already present. This ensures that the first list contains all unique elements from both lists without duplicates. For example, if a = [1, 2, 3] and b = [2, 3, 4, 5], the result should be [1, 2, 3, 4, 5]. Let's discuss different methods in which we can do this in Python.
Using set()
We can use a set() to store elements of the first list, allowing for faster lookups compared to checking membership in a list, which requires a linear search for each element.
Python
# Initializing lists
a = [1, 2, 3]
b = [2, 3, 4, 5]
# Using set for quick lookup
s = set(a)
for x in b:
if x not in s:
a.append(x)
s.add(x)
print(a)
Explanation:
- A set 's' is created from a for fast lookups.
- Missing elements from b are added to both a and s to maintain uniqueness.
Let's explore some more ways and see how we can append missing elements from other list.
Using Set Union
The set union() method provides a direct way to merge unique elements from both lists, ensuring no duplicates while efficiently merging them.
Python
# Initializing lists
a = [1, 2, 3]
b = [2, 3, 4, 5]
# Using set union
a = list(set(a) | set(b))
print(a)
Explanation:
- set(a) | set(b) merges unique elements from both lists.
- The result is converted back to a list.
itertools.chain() method combines both lists and dict.fromkeys() removes duplicates while maintaining order.
Python
from itertools import chain
# Initializing lists
a = [1, 2, 3]
b = [2, 3, 4, 5]
# Using itertools and dict to maintain order
a = list(dict.fromkeys(chain(a, b)))
print(a) # Output: [1, 2, 3, 4, 5]
Explanation:
- chain(a, b) joins both lists.
- dict.fromkeys() removes duplicates while preserving the original order.
Using List Comprehension
List comprehension allows us to filter out missing elements efficiently before appending them to the list..
Python
# Initializing lists
a = [1, 2, 3]
b = [2, 3, 4, 5]
# Extracting missing elements
a.extend([x for x in b if x not in a])
print(a)
Explanation:
- The list comprehension filters elements in b that are not in a.
- extend() is used to append them to a.
Using for Loop and in Operator
A simple way to check for missing elements and append them is by iterating through the second list.
Python
# Initializing lists
a = [1, 2, 3]
b = [2, 3, 4, 5]
# Appending missing elements
for x in b:
if x not in a:
a.append(x)
print(a)
Explanation:
- The for loop iterates through b and checks if each element is in a.
- If an element is missing, it is appended to a.
Similar Reads
Python | Find missing elements in List Sometimes, we can get elements in range as input but some values are missing in otherwise consecutive range. We might have a use case in which we need to get all the missing elements. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension We can perform the task o
7 min read
Append Elements to Empty List in Python In Python, lists are used to store multiple items in one variable. If we have an empty list and we want to add elements to it, we can do that in a few simple ways. The simplest way to add an item in empty list is by using the append() method. This method adds a single element to the end of the list.
2 min read
Add Elements of Two Lists in Python Adding corresponding elements of two lists can be useful in various situations such as processing sensor data, combining multiple sets of results, or performing element-wise operations in scientific computing. List Comprehension allows us to perform the addition in one line of code. It provides us a
3 min read
Python - Append Multiple elements in set In Python, sets are an unordered and mutable collection of data type what does not contains any duplicate elements. In this article, we will learn how to append multiple elements in the set at once. Example: Input: test_set = {6, 4, 2, 7, 9}, up_ele = [1, 5, 10]Output: {1, 2, 4, 5, 6, 7, 9, 10}Expla
4 min read
Python | Remove given element from list of lists The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
6 min read