Open In App

Python | Combine two lists by maintaining duplicates in first list

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two lists, the task is to combine two lists and removing duplicates, without removing duplicates in original list. 

Example:

Input : 
list_1 = [11, 22, 22, 15]
list_2 = [22, 15, 77, 9]

Output :
OutList = [11, 22, 22, 15, 77, 9]

Code #1: using extend 

Python3




# Python code to combine two lists
# and removing duplicates, without
# removing duplicates in original list.
 
# Initialisation of first list
list1 = [111, 222, 222, 115]
 
# Initialisation of Second list
list2 = [222, 115, 77, 19]
 
output = list(list1)
 
# Using extend function
output.extend(y for y in list2 if y not in output)
 
# printing result
print(output)


Output:

[111, 222, 222, 115, 77, 19]

Time complexity: O(n^2) as it uses nested loop to check each element of list2 with the elements of output list.
Auxiliary space: O(n) as it uses a separate list to store the combined elements without duplicates.

Code #2 : Using Set and Iteration Append those element in first list which are not in second list and then take union of first and second list. 

Python3




# Python code to combine two lists
# and removing duplicates, without
# removing duplicates in original list.
 
# Initialisation of first list
list1 = [11, 22, 22, 15]
 
# Initialisation of Second list
list2 = [22, 15, 77, 9]
 
# creating set
unique_list1 = set(list1)
unique_list2 = set(list2)
 
# Difference in two sets
diff_element = unique_list2 - unique_list1
 
# union of difference + first list
output = list1 + list(diff_element)
 
# printing output
print(output)


Output:

[11, 22, 22, 15, 9, 77]

Time complexity: O(n*n) as it uses nested loop to check each element of list2 with the elements of output list.
Auxiliary space: O(n) as it uses a separate list to store the combined elements without duplicates.

Code #3 : Using collectionss

This version first creates a Counter object for each of the lists, just like before. Then, it adds the two Counter objects using the + operator, but it only includes the elements from list2 that are not in list1. This is achieved using a generator expression that filters the elements in counter2 based on whether they are not in counter1. Finally, it converts the combined Counter object to a list using the elements() method, just like before.

Python3




from collections import Counter
 
# Initialize the lists
list1 = [11, 22, 22, 15]
list2 = [22, 15, 77, 9]
 
# Create a Counter object for each list
counter1 = Counter(list1)
counter2 = Counter(list2)
 
# Add the Counter objects, but only add the elements from list2 that are not in list1
combined_counter = counter1 + Counter(el for el in counter2 if el not in counter1)
 
# Convert the combined Counter object to a list
output = list(combined_counter.elements())
 
# Print the output
print(output)
#This code is contributed by Edula Vinay Kumar Reddy


Output

[11, 22, 22, 15, 77, 9]

Time complexity: O(n), where n is the total number of elements in the two lists. This is because the Counter class uses a dictionary data structure to store the elements and their counts, and the time complexity of the Counter operations (e.g. creating a Counter object, adding two Counter objects, and converting a Counter object to a list) is O(1) on average.
Auxiliary space: O(n), because the combined Counter object will store a count for each element in the two lists, and the resulting list will contain all the elements from the two lists.

Method 4: Using loop + append()

Uses a for loop to iterate over the elements of the second list, and then it checks if each element is already present in the output list using the not in operator. If it’s not present, it appends it to the output list using the append() method. This approach essentially builds a new list that combines the elements of both input lists while removing duplicates, without modifying the original lists. This approach is similar to the original program, but it explicitly makes a copy of the first list before iterating over the second list.

  • Define two input lists list1 and list2 with some initial values.
  • Create a copy of list1 using the list() constructor and assign it to a new list variable called output.
  • Use a for loop to iterate over the elements of list2. For each element y:
    • Check if y is already present in output using the not in operator.
    • If y is not present in output, append it to output using the append() method.
  • Print the resulting list output.

Implementation:

Python3




# Python code to combine two lists
# and removing duplicates, without
# removing duplicates in original list.
 
# Initialisation of first list
list1 = [111, 222, 222, 115]
 
# Initialisation of Second list
list2 = [222, 115, 77, 19]
 
# Make a copy of list1 to avoid modifying the original list
output = list(list1)
 
# Iterate over the elements of list2 and append them to the output list
# only if they are not already present in it
for y in list2:
    if y not in output:
        output.append(y)
 
# Print the resulting list
print(output)


Output

[111, 222, 222, 115, 77, 19]

Time Complexity: O(m*n),  where m and n are the lengths of the two input lists list1 and list2
Auxiliary Space: O(m + n)

Using numpy:

Note: Install numpy module using command “pip install numpy”

Approach:

In this approach, we will use the numpy.setdiff1d function to find the unique elements of arr2 that are not present in arr1. We will then convert the resulting array to a list and append it to list1.

Algorithm:

Import numpy library.
Define list1 and arr2 with some initial values.
Use np.setdiff1d function to find unique elements of arr2 that are not in arr1 and assign the result to arr2_unique.
Convert arr2_unique to a list using the tolist() method and append it to list1.
Print the resulting list.

Python3




import numpy as np
 
# Define input lists
list1 = [11, 22, 22, 15]
arr2 = np.array([22, 15, 77, 9])
 
# Find unique elements of arr2 that are not in list1
arr2_unique = np.setdiff1d(arr2, list1, assume_unique=True)
 
# Append arr2_unique to list1
result_list = list1 + arr2_unique.tolist()
 
# Print the resulting list
print(result_list)


Output:
[11, 22, 22, 15, 77, 9]

Time complexity:

Creating a NumPy array takes O(n) time where n is the length of the input list.
np.setdiff1d function takes O(n*log(n)) time for sorting and finding the unique elements.
tolist() method takes O(n) time to convert the NumPy array to a list.
Appending two lists takes O(n) time where n is the total length of the lists.
Therefore, the overall time complexity of this approach is O(n*log(n)).

Auxiliary Space:

Creating a NumPy array takes O(n) space where n is the length of the input list.
The arr2_unique array created by np.setdiff1d function takes O(m) space where m is the number of unique elements in arr2 that are not in list1.
The result_list list takes O(n+m) space to store the combined elements of list1 and arr2_unique.
Therefore, the overall space complexity of this approach is O(n+m).



Similar Reads

How To Combine Multiple Lists Into One List Python
Combining multiple lists into a single list is a common operation in Python, and there are various approaches to achieve this task. In this article, we will see how to combine multiple lists into one list in Python. Combine Multiple Lists Into One List PythonBelow are some of the ways by which we can see how we can combine multiple lists into one l
2 min read
Python | Inserting item in sorted list maintaining order
Working with sorted list is essential because mostly the data we get is ordered. Any query can come to insert the new data into its appropriate position. Hence requires to know how to perform these dynamic queries. Lets discuss certain ways in which this can be performed. Method #1 : Naive Method In this method, we just simply test for the value an
6 min read
Python | Maintaining Grocery list
Problem Statement : Make a Grocery List for super market shopping with name, price and quantity; if the list already contains an item then only update the price and quantity it should not append the item name again. Ask user his/her budget initially and minus the budget after adding a new item in the list. If budgets go zero/0 then no more item cou
5 min read
Combine Two Flat Lists into a 2D Array
Combining two flat lists into a 2D array is a common task in programming, especially when dealing with data manipulation and analysis. In this article, we will explore four generally used and simple methods to achieve this goal. Whether you are working with Python, JavaScript, or any other programming language, these methods can be adapted to suit
3 min read
Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
Given two dictionaries. The task is to merge them in such a way that the resulting dictionary contains the key from the first dictionary and the value from the second dictionary. Examples: Input : test_dict1 = {"Gfg" : 20, "is" : 36, "best" : 100}, test_dict2 = {"Gfg2" : 26, "is2" : 20, "best2" : 70} Output : {'Gfg': 26, 'is': 20, 'best': 70} Expla
8 min read
Python | Pair and combine nested list to tuple list
Sometimes we need to convert between the data types, primarily due to the reason of feeding them to some function or output. This article solves a very particular problem of pairing like indices in list of lists and then construction of list of tuples of those pairs. Let's discuss how to achieve the solution of this problem. Method #1 : Using zip()
10 min read
Python - Combine list with other list elements
Given two lists, combine list with each element of the other list. Examples: Input : test_list = [3, 5, 7], pair_list = ['Gfg', 'is', 'best'] Output : [([3, 5, 7], 'Gfg'), ([3, 5, 7], 'is'), ([3, 5, 7], 'best')] Explanation : All lists paired with each element from other list. Input : test_list = [3, 5, 7], pair_list = ['Gfg', 'best'] Output : [([3
6 min read
Python | Difference of two lists including duplicates
The ways to find difference of two lists has been discussed earlier, but sometimes, we require to remove only the specific occurrences of the elements as much they occur in other list. Let's discuss certain ways in which this can be performed. Method #1 : Using collections.Counter() The Counter method can be used to get the exact occurrence of the
5 min read
Python Merge Two Lists Without Duplicates
In Python coding, combining two lists without repeating elements can be a tricky task. It involves skillfully managing data structures to achieve a smooth and effective outcome. In this short guide, we'll explore ways to merge lists in Python, making sure duplicates are handled seamlessly. Python Merge Two Lists Without DuplicatesBelow, are the met
3 min read
Python | Combine the values of two dictionaries having same key
Dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. It is widely used in day to day programming, web development, and machine learning. Combining dictionaries is very common task in operations of dictionary. Let's see how to combine the values
7 min read