Python – Check if tuple list has all K
Last Updated :
27 Feb, 2023
Sometimes, while working with Python records, we can have a problem in which we need to test if all the elements in tuples of tuple list are K. This problem can have applications in many data domains such as Machine Learning and Web development. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [(4, 4, 4, 4)], K = 4
Output : True
Input : test_list = [(7), (5, ), (5, ), (5, )], K = 5
Output : False
Method #1 : Using loop This is brute force way in which this task can be performed. In this, we use loop to iterate each value in tuple and test if its K, if we find any element to be non-K, False is returned.
Python3
test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )]
print ( "The original list is : " + str (test_list))
K = 4
res = True
for tup in test_list:
for ele in tup:
if ele ! = K:
res = False
print ( "Are all elements K ? : " + str (res))
|
Output
The original list is : [(4, 4), (4, 4, 4), (4, 4), (4, 4, 4, 4), (4,)]
Are all elements K ? : True
Method #2 : Using all() + any() This is yet another way in which this question can be answered. In this, we check for all elements to be K using all() and check if any of them doesn’t follow this behavior by using outer any().
Python3
test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )]
print ( "The original list is : " + str (test_list))
K = 4
res = any ( all (val = = K for val in tup) for tup in test_list)
print ( "Are all elements K ? : " + str (res))
|
Output
The original list is : [(4, 4), (4, 4, 4), (4, 4), (4, 4, 4, 4), (4,)]
Are all elements K ? : True
Method #3 : Using extend(),list(),count() and len() methods
Approach
- Initiated a for loop to traverse over the list of tuples
- Convert each tuple element to list
- And extend all list to a new list
- Now check the count of K in the list is equal to the length of list(using count())
- If equal assign True to res
- Display res
Python3
test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )]
print ( "The original list is : " + str (test_list))
K = 4
res = False
x = []
for i in test_list:
i = list (i)
x.extend(i)
if (x.count(K) = = len (x)):
res = True
print ( "Are all elements K ? : " + str (res))
|
Output
The original list is : [(4, 4), (4, 4, 4), (4, 4), (4, 4, 4, 4), (4,)]
Are all elements K ? : True
Method #4 : Using extend(),list(),operator.countOf() and len() methods
Approach
- Initiated a for loop to traverse over the list of tuples
- Convert each tuple element to list
- And extend all list to a new list
- Now check the count of K in the list is equal to the length of list(using operator.countOf())
- If equal assign True to res
- Display res
Python3
test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )]
print ( "The original list is : " + str (test_list))
K = 4
res = False
x = []
for i in test_list:
i = list (i)
x.extend(i)
import operator
if (operator.countOf(x,K) = = len (x)):
res = True
print ( "Are all elements K ? : " + str (res))
|
Output
The original list is : [(4, 4), (4, 4, 4), (4, 4), (4, 4, 4, 4), (4,)]
Are all elements K ? : True
Time Complexity : O(N)
Auxiliary Space : O(1)
Method #5 : Using extend(),list(),set() and len() methods
Python3
test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )]
print ( "The original list is : " + str (test_list))
K = 4
res = False
x = []
for i in test_list:
i = list (i)
x.extend(i)
a = set (x)
if ( len (a) = = 1 ):
res = True
print ( "Are all elements K ? : " + str (res))
|
Output
The original list is : [(4, 4), (4, 4, 4), (4, 4), (4, 4, 4, 4), (4,)]
Are all elements K ? : True
Method #6: Using filter()+list()+ lambda functions
Python3
test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )]
print ( "The original list is : " + str (test_list))
K = 4
res = True
for tup in test_list:
res = len ( list ( filter ( lambda x: x ! = K, tup))) = = 0
if ( not res):
break
print ( "Are all elements K ? : " + str (res))
|
Output
The original list is : [(4, 4), (4, 4, 4), (4, 4), (4, 4, 4, 4), (4,)]
Are all elements K ? : True
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #7: Using recursive method.
Python3
def all_k(start,lst,K):
if start = = len (lst):
return True
if not all ( map ( lambda x:x = = K,lst[start])):
return False
return all_k(start + 1 ,lst,K)
test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )]
print ( "The original list is : " + str (test_list))
K = 4
res = all_k( 0 ,test_list,K)
print ( "Are all elements K ? : " + str (res))
|
Output
The original list is : [(4, 4), (4, 4, 4), (4, 4), (4, 4, 4, 4), (4,)]
Are all elements K ? : True
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method#8 : Using Set() function
Python3
test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )]
print ( "The original list is : " + str (test_list))
K = 4
res = False
if all ( set (tup) = = {K} for tup in test_list):
res = True
print ( "Are all elements K ? : " + str (res))
|
Output
The original list is : [(4, 4), (4, 4, 4), (4, 4), (4, 4, 4, 4), (4,)]
Are all elements K ? : True
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...