To remove duplicates from a list of lists in Python, you can use two ways: using the sorted() function in combination with the set() function and using the map() function in combination with the set() function. Read the following article for more information.
Remove duplicates from a list of lists in Python
Use the sorted() function
Syntax:
sorted(object, key=None, reverse=False)
Parameters:
- object: a string, an interpolator, or an iterator object.
- key: sorted() function with key value, then sort. The default is none.
- reverse: default value is False. If reverse = True, the list will sort reverse.
The sorted() function in Python sorts the elements of an object in a particular order (ascending or descending) and returns the object as a sorted list.
Example:
- Initialize a list of arrays where some of the arrays are the same.
- Use the sorted() function to sort the list.
- Convert the list of tuples to a set to eliminate duplicates.
- Note: Remember to convert the nested list to a tuple before converting it to a set because lists are mutable and non-hashable and cannot be a member of a set.
- Finally, use the list() function to return the results to a list.
# The list consists of sublists myList = [[0, 0, 1], [1, 1, 0], [1, 1, 0], [1, 2, 3], [4, 5, 6]] # Use the sorted() and set() function result = list(set(tuple(sorted(i)) for i in myList)) print("List after removing the same sublists: ", result)
Output:
List after removing the same sublists: [(1, 2, 3), (0, 0, 1), (4, 5, 6), (0, 1, 1)]
Use the map() and set() function
Syntax:
map(func,iterable)
Parameters:
- func: given function to iterate over the elements of iterable.
- iterable: the object you want to browse.
Example:
- Initialize a list of arrays where some of the arrays are the same.
- The map function converts the list to a tuple object that can be hashed (so that the list is a set member).
- Use the set() function to remove duplicates of the same.
- Finally, use the list() function to return the results to a list.
# The list consists of sublists myList = [[0, 0, 1], [1, 1, 0], [1, 1, 0], [1, 2, 3], [4, 5, 6]] # Use the map() and set() functions to remove duplicates from a list of lists result=list(set(map(tuple,myList))) print("List after removing the same sublists: ", result)
Output:
List after removing the same sublists: [(1, 2, 3), (0, 0, 1), (1, 1, 0), (4, 5, 6)]
Summary
Thank you for being so interested in my article. Hope you got an idea for the topic of how to remove duplicates from a list of lists in Python. You can use both ways for this theme. Let us know if the article is flawed or if you have other ideas.
Maybe you are interested:
- Remove an element from a list by index in python
- Remove \r\n from a string or list of strings in Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java