- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Test if all elements are unique in columns of a Matrix
When it is required to test if all elements are unique in columns of a matrix, a simple iteration and a list comprehension along with the ‘set’ operator are used.
Below is a demonstration of the same −
my_list = [[11, 24, 84], [24, 55, 11], [7, 11, 9]] print("The list is :") print(my_list) my_result = True for index in range(len(my_list)): column = [ele[index] for ele in my_list] if len(list(set(column ))) != len(column ): my_result = False break if(my_result == True): print("All columns are unique") else: print(("All columns are not unique"))
The list is : [[11, 24, 84], [24, 55, 11], [7, 11, 9]] All columns are unique
A list of list with integers is defined and is displayed on the console.
A variable is assigned Boolean value ‘True’.
The list is iterated over, and list comprehension is used to find index of element.
If a specific condition is fulfilled, i.e if the length of unique elements in the list is not equal to the length of the elements, the Boolean value is initialized to ‘False’.
The control breaks out of the loop.
In the end, depending on the Boolean value, the relevant message is displayed on the console.
- Check if list contains all unique elements in Python
- Python - Check if all elements in a List are same
- Python - Check if all elements in a list are identical
- Check if all array elements are distinct in Python
- Check if all rows of a matrix are circular rotations of each other in Python
- Python – Test if all rows contain any common element with other Matrix
- Program to find the product of three elements when all are unique in Python
- Python – Test if elements of list are in Min/Max range from other list
- Check if all enemies are killed with bombs placed in a matrix in Python
- Find distinct elements common to all rows of a matrix in Python
- Check if all elements of the array are palindrome or not in Python
- Python program to check if a string contains all unique characters
- Count all the columns in a matrix which are sorted in descending in C++
- Check if all bits of a number are set in Python