Given a binary matrix, print all unique rows of the given matrix. Order of row printing doesn’t matter.
Examples:
Input:
mat = [[0, 1, 0, 0, 1],
[1, 0, 1, 1, 0],
[0, 1, 0, 0, 1],
[1, 1, 1, 0, 0]]
Output:
(1, 1, 1, 0, 0)
(0, 1, 0, 0, 1)
(1, 0, 1, 1, 0)
We have existing solution for this problem please refer link. We can solve this problem in python quickly using Set data structure. Approach is very simple.
- We are given list of boolean values list, put all rows (list) in set because set contains unique values.
- Since list is an unhashable type for set because it is mutable that’s why first we convert each row (list) into tuple then we put all tuple in set.
- Resultant set will contain only unique valued tuples (row).
# Python program to Print unique rows in a # given boolean matrix using Set with tuples # Function to print unique rows in a given boolean matrix def uniqueRows(input): # convert each row (list) into tuple # we are mapping tuple function on each row of # input matrix input = map(tuple, input) # put all rows in set result = set(input) # print all unique rows for row in list(result): print (row) # Driver program if __name__ == "__main__": input = [[0, 1, 0, 0, 1], [1, 0, 1, 1, 0], [0, 1, 0, 0, 1], [1, 1, 1, 0, 0]] uniqueRows(input) |
Output:
(1, 1, 1, 0, 0) (0, 1, 0, 0, 1) (1, 0, 1, 1, 0)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print unique rows in a given boolean matrix
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python - Filter all uppercase characters Tuples from given list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python | Convert string tuples to list tuples
- Python | How to Concatenate tuples to nested tuples
- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Check if the rows of a binary matrix can be made unique by removing a single column
- Python | Get unique tuples from list
- Python | Extract unique tuples from list, Order Irrespective
- Python - Unique Kth positioned tuples
- Python - Filter unique valued tuples
- Find unique rows in a NumPy array
- Find the number of rows and columns of a given matrix using NumPy
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

