Open In App

Python – Append Missing Elements from Other List

Last Updated : 01 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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)

Output
[1, 2, 3, 4, 5]

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)

Output
[1, 2, 3, 4, 5]

Explanation:

  • set(a) | set(b) merges unique elements from both lists.
  • The result is converted back to a list.

Using itertools.chain()

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]  

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)

Output
[1, 2, 3, 4, 5]

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)  

Output
[1, 2, 3, 4, 5]

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.


Next Article
Practice Tags :

Similar Reads