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 task can be performed in brute force manner using loops. In this, we just iterate the list of list using loop and check for the already presence of element, and append in case it’s new element, and construct a non-duplicate matrix.





# Python3 code to demonstrate working of
# Removing duplicates in Matrix
# using loop
# initialize list 
test_list = [[5, 6, 8], [8, 5, 3], [9, 10, 3]]
# printing original list
print("The original list is : " + str(test_list))
# Removing duplicates in Matrix
# using loop
res = []
track = []
count = 0
for sub in test_list:
    for ele in sub:
        if ele not in track:
    count += 1
# printing result
print("The Matrix after duplicates removal is : " + str(res))


Output :

The original list is : [[5, 6, 8], [8, 5, 3], [9, 10, 3]]
The Matrix after duplicates removal is : [[5, 6, 8], [3], [9, 10]]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.