Python | Combine two lists by maintaining duplicates in first list
Last Updated :
02 May, 2023
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
Python - Merging duplicates to list of list
We are given a list we need to merge the duplicate to lists of list. For example a list a=[1,2,2,3,4,4,4,5] we need to merge all the duplicates in lists so that the output should be [[2,2],[4,4,4]]. This can be done by using multiple functions like defaultdict from collections and Counter and variou
3 min read
Python - Count of matching elements among lists (Including duplicates)
Given 2 lists, count all the elements that are similar in both the lists including duplicated. Input : test_list1 = [3, 5, 6, 7, 2, 3, 5], test_list2 = [5, 5, 3, 9, 8, 5] Output : 4 Explanation : 3 repeats 2 times, and 5 two times, totalling to 4. Input : test_list1 = [3, 5, 6], test_list2 = [5, 3,
4 min read
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 ca
2 min read
Python - Difference of Two Lists Including Duplicates
We are given two list we need find difference between two list. For example, a = [1, 2, 2, 3, 4] and b = [2, 3] we need to find difference of two list so that resultant output should be [1, 2, 4].Using a for loopA for loop can iterate through first list and remove matching elements from second list
2 min read
Python - Convert List of Dictionaries to List of Lists
We are given list of dictionaries we need to convert it to list of lists. For example we are given a list of dictionaries a = [{'name': 'Geeks', 'age': 25}, {'name': 'Geeks', 'age': 30}] we need to convert it in list of list so that the output becomes[['Geeks',25],['Geeks;'30]].Using List Comprehens
3 min read
Python | Merge two list of lists according to first element
Given two list of lists of equal length, write a Python program to merge the given two lists, according to the first common element of each sublist. Examples: Input : lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']] lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']] Output : [[1, 'Alice', 'Delhi'],
6 min read
Remove all Duplicates and Permutations in Nested List - Python
Given a nested list the task is to remove all duplicates and permutations in that nested list. For example: we have the nested list : [[ -11, 0, 11], [ -11, 11, 0], [ -11, 0, 11], [-11, 2, -11], [-11, 2, -11], [-11, -11, 2]] then output will be {(-11, 0, 11), (-11, -11, 2)}Using MapWe first sort eac
3 min read
Python - Convert List to List of dictionaries
We are given a lists with key and value pair we need to convert the lists to List of dictionaries. For example we are given two list a=["name", "age", "city"] and b=[["Geeks", 25, "New York"], ["Geeks", 30, "Los Angeles"], ["Geeks", 22, "Chicago"]] we need to convert these keys and values list into
4 min read
Combine keys in a list of dictionaries in Python
Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform a merge of dictionaries in list with similar keys. This kind of problem can come in data optimization domains. Let's discuss a way in which this task can be performed. Input : test_list = [{'a': 6},
2 min read
How to Find Duplicates in a List - Python
Finding duplicates in a list is a common task in programming. In Python, there are several ways to do this. Letâs explore the efficient methods to find duplicates. Using a Set (Most Efficient for Large Lists)Set() method is used to set a track seen elements and helps to identify duplicates. Pythona
2 min read