Skip to content
Related Articles

Related Articles

Python – Test if all elements are unique in columns in a Matrix

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 12 Nov, 2020

Given a Matrix, test if all columns contain unique elements.

Input : test_list = [[3, 4, 5], [1, 2, 4], [4, 1, 10]] 
Output : True 
Explanation : 3, 1, 4; 4, 2, 1; 5, 4, 10; All elements are unique in columns.

Input : test_list = [[3, 4, 5], [3, 2, 4], [4, 1, 10]] 
Output : False 
Explanation : 3, 3, 4; 3 repeated twice. 
 

Method #1 : Using loop + set() + len()

In this, we iterate for each column and test for unique elements using set size using len(), if any column is found having a size not equal to the actual list, then the result is flagged off.

Python3




# Python3 code to demonstrate working of 
# Test if all elements Unique in Matrix Columns
# Using loop + set() + len()
  
# initializing list
test_list = [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
  
# printing original lists
print("The original list is : " + str(test_list))
  
res = True 
for idx in range(len(test_list[0])):
      
    # getting column 
    col = [ele[idx] for ele in test_list]
      
    # checking for all Unique elements
    if len(list(set(col))) != len(col):
        res = False 
        break
  
# printing result 
print("Are all columns Unique : " + str(res))

Output:

The original list is : [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
Are all columns Unique : True

Method #2 : Using all() + list comprehension

This can be solved in one-liner using all() which checks for all the columns,  made using list comprehension, if all columns return True, all() returns true.

Python3




# Python3 code to demonstrate working of 
# Test if all elements Unique in Matrix Columns
# Using loop + set() + len()
  
# initializing list
test_list = [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
  
# printing original lists
print("The original list is : " + str(test_list))
  
res = True 
for idx in range(len(test_list[0])):
      
    # getting column 
    col = [ele[idx] for ele in test_list]
      
    # checking for all Unique elements
    if len(list(set(col))) != len(col):
        res = False 
        break
  
# printing result 
print("Are all columns Unique : " + str(res))

Output:

The original list is : [[3, 4, 5], [1, 2, 4], [4, 1, 10]]
Are all columns Unique : True


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!