Python | Remove duplicates from nested list
Last Updated :
15 May, 2023
The task of removing duplicates many times in the recent past, but sometimes when we deal with the complex data structure, in those cases we need different techniques to handle this type of problem. Let's discuss certain ways in which this task can be achieved.
Method #1 : Using sorted() + set()
This particular problem can be solved using the above functions. The idea here is to sort the sublist and then remove the like elements using the set operations which removes duplicates.
Python3
# Python3 code to demonstrate
# removing duplicate sublist
# using set() + sorted()
# Initializing list
test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],
[1, 2, 3], [3, 4, 1]]
# Printing original list
print("The original list : " + str(test_list))
# Removing duplicate sublist
# using set() + sorted()
res = list(set(tuple(sorted(sub)) for sub in test_list))
# Printing result
print("The list after duplicate removal : " + str(res))
Output : The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
The list after duplicate removal : [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]
Time complexity: O(nmlog(m)), where n is the number of sublists and m is the length of each sublist. This is because we are using the sorted() function which takes O(m*log(m)) time to sort each sublist and we are using this function for each sublist.
Auxiliary space: O(nm), where n is the number of sublists and m is the length of each sublist. This is because we are creating a tuple of each sublist and storing it in a set, so the space required would be O(nm).
Method #2: Using set() + map() + sorted()
The task performed by the list comprehension in the above method can be modified using the map function using lambda functions to extend the logic to each and every sublist.
Python3
# Python3 code to demonstrate
# removing duplicate sublist
# using set() + map() + sorted()
# Initializing list
test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],
[1, 2, 3], [3, 4, 1]]
# Printing original list
print("The original list : " + str(test_list))
# Removing duplicate sublist
# using set() + map() + sorted()
res = list(set(map(lambda i: tuple(sorted(i)), test_list)))
# Printing result
print("The list after duplicate removal : " + str(res))
Output : The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
The list after duplicate removal : [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]
Time complexity: O(nmlog(m)), where n is the number of sublists and m is the length of each sublist. This is because we are using set() + map() + sorted() which has a time complexity of O(n*n) in the worst case.
Auxiliary space: O(nm), where n is the number of sublists and m is the length of each sublist. This is because we are creating a tuple of each sublist and storing it in a set, so the space required would be O(nm).
Method #3 : Using sorted() method ,in, not in operators
Python3
# Python3 code to demonstrate
# removing duplicate sublist
# Initializing list
test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],
[1, 2, 3], [3, 4, 1]]
# Printing original list
print("The original list : " + str(test_list))
# Removing duplicate sublist
# Empty list
res1 = []
for i in test_list:
x=sorted(i)
res1.append(x)
res=[]
for i in res1:
if tuple(i) not in res:
res.append(tuple(i))
# Printing result
print("The list after duplicate removal : " + str(res))
OutputThe original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
The list after duplicate removal : [(-1, 0, 1), (1, 2, 3), (1, 3, 4)]
Method #4: Using Numpy
This method makes use of the numpy library's unique() function to remove duplicates from the nested list.
- We will be using a list comprehension to sort each sublist using the numpy.sort() function.
- Convert the resulting list of arrays to a numpy array using np.array()
- Using the np.unique() function to remove duplicates along the rows (axis=0).
- The resulting array is then converted back to a list using the tolist() method.
Example:
Python3
# Importing numpy module
import numpy as np
# Initializing nested list
test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
# Printing original list
print("The original list : " + str(test_list))
# Removing duplicates
# using numpy
res = np.unique(np.array([np.sort(sub) for sub in test_list]), axis=0)
# Printing result
print("The list after duplicate removal : " + str(res.tolist()))
Output:
The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
The list after duplicate removal : [[-1, 0, 1], [1, 2, 3], [1, 3, 4]]
Time Complexity: O(nmlog(m)), where n is the number of sublists and m is the length of each sublist. This is because we are first converting each sublist to a tuple using a list comprehension which takes O(nm) time, and then passing the resulting list of tuples to the unique() function. The unique() function sorts the array of tuples, which takes O(nm*log(m)) time.
Auxiliary Space: O(nm), where n is the number of sublists and m is the length of each sublist. This is because we are first converting each sublist to a tuple using a list comprehension which takes O(nm) space, and then passing the resulting list of tuples to the unique() function.
Similar Reads
Python - Remove Duplicates from a List
Removing duplicates from a list is a common operation in Python which is useful in scenarios where unique elements are required. Python provides multiple methods to achieve this. Using set() method is most efficient for unordered lists. Converting the list to a set removes all duplicates since sets
2 min read
Remove Duplicate Strings from a List in Python
Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this. set() method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values.Pythona = ["Learn", "Python", "With"
3 min read
Remove empty Lists from List - Python
In this article, we will explore various method to remove empty lists from a list. The simplest method is by using a for loop.Using for loopIn this method, Iterate through the list and check each item if it is empty or not. If the list is not empty then add it to the result list.Pythona = [[1, 2], [
2 min read
Python - Remove Duplicates from a list And Keep The Order
While lists provide a convenient way to manage collections of data, duplicates within a list can sometimes pose challenges. In this article, we will explore different methods to remove duplicates from a Python list while preserving the original order.Using dict.fromkeys()dict.fromkeys() method creat
2 min read
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x
5 min read
Python | Remove duplicates in Matrix
While working with Python Matrix, we can face a problem in which we need to perform the removal of duplicates from Matrix. This problem can occur in Machine Learning domain because of extensive usage of matrices. Let's discuss certain way in which this task can be performed. Method : Using loop This
2 min read
Python | Duplicate substring removal from list
Sometimes we can come to the problem in which we need to deal with certain strings in a list that are separated by some separator and we need to remove the duplicates in each of these kinds of strings. Simple shorthands to solve this kind of problem is always good to have. Let's discuss certain ways
7 min read
Python | Removing duplicates from tuple
Many times, while working with Python tuples, we can have a problem removing duplicates. This is a very common problem and can occur in any form of programming setup, be it regular programming or web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using set()
4 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 - Remove duplicate words from Strings in List
Sometimes, while working with Python list we can have a problem in which we need to perform removal of duplicated words from string list. This can have application when we are in data domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + split() + loop The
6 min read