Python – Test if all elements are unique in columns in a Matrix
Last Updated :
27 Mar, 2023
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
test_list = [[ 3 , 4 , 5 ], [ 1 , 2 , 4 ], [ 4 , 1 , 10 ]]
print ( "The original list is : " + str (test_list))
res = True
for idx in range ( len (test_list[ 0 ])):
col = [ele[idx] for ele in test_list]
if len ( list ( set (col))) ! = len (col):
res = False
break
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
Time Complexity: O(n*m)
Auxiliary Space: O(k)
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
test_list = [[ 3 , 4 , 5 ], [ 1 , 2 , 4 ], [ 4 , 1 , 10 ]]
print ( "The original list is : " + str (test_list))
res = True
for idx in range ( len (test_list[ 0 ])):
col = [ele[idx] for ele in test_list]
if len ( list ( set (col))) ! = len (col):
res = False
break
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
Time Complexity: O(n) where n is the number of elements in the list “test_list”. The time complexity of the all() and list comprehension is O(n)
Auxiliary Space: O(1), no extra space is required
Method #3 : Using loop + Counter() function
Python3
from collections import Counter
test_list = [[ 3 , 4 , 5 ], [ 1 , 2 , 4 ], [ 4 , 1 , 10 ]]
print ( "The original list is : " + str (test_list))
res = True
for idx in range ( len (test_list[ 0 ])):
col = []
for ele in test_list:
col.append(ele[idx])
freq = Counter(col)
if ( len (freq.keys()) ! = len (col)):
res = False
break
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
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
Method #4: Using numpy:
Algorithm:
- Initialize the list to check for uniqueness in columns.
- For each column in the transposed matrix of the input list, check if the number of unique elements is the same as the length of the column.
- If any column is found to have duplicate elements, set the result to False and break the loop.
- If all columns have unique elements, set the result to True.
- Print the result.
Python3
import numpy as np
test_list = [[ 3 , 4 , 5 ], [ 1 , 2 , 4 ], [ 4 , 1 , 10 ]]
print ( "The original list is : " + str (test_list))
res = all ( len (np.unique(col)) = = len (col) for col in zip ( * test_list))
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
Time complexity: O(nm log m)
where n is the number of rows and m is the number of columns in the input matrix. This is because we are iterating over each column of the transposed matrix and checking if the length of the unique elements in each column is equal to the length of the column. The time complexity of np.unique() function is O(m log m) where m is the number of elements in the array.
Auxiliary Space: O(m)
where m is the number of columns in the input matrix. This is because we are storing each column temporarily in the list comprehension before checking for uniqueness. Additionally, the space complexity of the np.unique() function is O(m).
Similar Reads
Python | Check if all elements in a List are same
To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. [GFGTABS] Python a = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) [/GFGTABS]OutputTrue Ex
3 min read
Test if all elements are present in list-Python
The task of testing if all elements are present in a list in Python involves checking whether every item in a target list exists within a reference list. For example, given two lists a = [6, 4, 8, 9, 10] and b = [4, 6, 9], the task is to confirm that all elements in list b are also found in list a.
3 min read
Python - Test if all rows contain any common element with other Matrix
Given two Matrices, check if all rows contain at least one element common with the same index row of other Matrix. Input : test_list1 = [[5, 6, 1], [2, 4], [9, 3, 5]], test_list2 = [[9, 1, 2], [9, 8, 2], [3, 7, 10]] Output : True Explanation : 1, 2 and 3 are common elements in rows. Input : test_lis
5 min read
Python | Check if all elements in a list are identical
Given a list, write a Python program to check if all the elements in that list are identical using Python. Examples: Input : ['a', 'b', 'c'] Output : False Input : [1, 1, 1, 1] Output : TrueCheck if all elements in a list are identical or not using a loop Start a Python for loop and check if the fir
5 min read
Search Elements in a Matrix - Python
The task of searching for elements in a matrix in Python involves checking if a specific value exists within a 2D list or array. The goal is to efficiently determine whether the desired element is present in any row or column of the matrix. For example, given a matrix a = [[4, 5, 6], [10, 2, 13], [1
3 min read
Python | Remove Initial K column elements
Sometimes, while working with Matrix data, we can have stray elements that attached at front end of each row of matrix. This can be undesired at times and wished to be removed. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop + del + list slicing The combination
4 min read
Python - Check Similar elements in Matrix rows
Given a Matrix and list, the task is to write a Python program to check if all the matrix elements of the row are similar to the ith index of the List. Input : test_list = [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Output : True Explanation : All rows have same elements.Input : test_list = [[1, 1,
8 min read
Python - Check if any list element is present in Tuple
Given a tuple, check if any list element is present in it. Input : test_tup = (4, 5, 10, 9, 3), check_list = [6, 7, 10, 11] Output : True Explanation : 10 occurs in both tuple and list. Input : test_tup = (4, 5, 12, 9, 3), check_list = [6, 7, 10, 11] Output : False Explanation : No common elements.
6 min read
Python - Check if Tuple contains only K elements
Sometimes, while working with Python tuples, we can have a problem in which we need to test if any tuple contains elements from set K elements. This kind of problem is quite common and have application in many domains such as web development and day-day programming. Let's discuss certain ways in whi
3 min read
Python Check if the List Contains Elements of another List
The task of checking if a list contains elements of another list in Python involves verifying whether all elements from one list are present in another list. For example, checking if ["a", "b"] exists within ["a", "b", "c", "d"] would return True, while checking ["x", "y"] would return False. Using
3 min read