One method is to convert the list of lists into a set of tuples, where each tuple represents a sublist. Since sets can only contain unique elements, this will automatically remove any duplicate sublists. Then, the set can be converted back into a list of lists. Here's an example code:
lst = [[1, 2], [3, 4], [1, 2], [5, 6], [3, 4]]
lst_set = set(tuple(sublist) for sublist in lst)
new_lst = [list(tple) for tple in lst_set]
print(new_lst)
# Output: [[1, 2], [3, 4], [5, 6]]
In this example, the original list lst
contains two identical sublists: [1, 2]
and [3, 4]
. After converting the list to a set of tuples and back to a list of lists, the duplicates have been removed, and new_lst
only contains [1, 2]
, [3, 4]
, and [5, 6]
.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-15 03:43:27 +0000
Seen: 9 times
Last updated: Jul 15 '23
What is the reason that this slice with negative index produces a list with no elements?
What is the process to detect and count common elements in R?
How can you space out HTML elements using CSS exclusively?
Can an Array of Elements be made in Android?
What is the method to show a line break <br> between <div> elements in Laravel?
When does the ForEach loop come to an end? Does it happen when a null value is returned?