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
# Python3 code to demonstrate working of # Check if tuple list has all K # Using loop # initializing list test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 # Check if tuple list has all K # Using loop res = True for tup in test_list: for ele in tup: if ele ! = K: res = False # printing result print ( "Are all elements K ? : " + str (res)) |
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
# Python3 code to demonstrate working of # Check if tuple list has all K # Using all() + any() # initializing list test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 # Check if tuple list has all K # Using all() + any() res = any ( all (val = = K for val in tup) for tup in test_list) # printing result print ( "Are all elements K ? : " + str (res)) |
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
# Python3 code to demonstrate working of # Check if tuple list has all K # initializing list test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 # Check if tuple list has all K # Using loop res = False x = [] for i in test_list: i = list (i) x.extend(i) if (x.count(K) = = len (x)): res = True # printing result print ( "Are all elements K ? : " + str (res)) |
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
# Python3 code to demonstrate working of # Check if tuple list has all K # initializing list test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 # Check if tuple list has all K # Using loop 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 # printing result print ( "Are all elements K ? : " + str (res)) |
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
# Python3 code to demonstrate working of # Check if tuple list has all K # Initializing list test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )] # Printing original list print ( "The original list is : " + str (test_list)) # Initializing K K = 4 # Check if tuple list has all K # Using loop res = False x = [] for i in test_list: i = list (i) x.extend(i) a = set (x) if ( len (a) = = 1 ): res = True # Printing result print ( "Are all elements K ? : " + str (res)) |
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
# Python3 code to demonstrate working of # Check if tuple list has all K # initializing list test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 # Check if tuple list has all K # Using loop res = True for tup in test_list: res = len ( list ( filter ( lambda x: x ! = K, tup))) = = 0 if ( not res): break # printing result print ( "Are all elements K ? : " + str (res)) |
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
# Python3 code to demonstrate working of # Check if tuple list has all K #defining recursive method def all_k(start,lst,K): if start = = len (lst): #base condition return True if not all ( map ( lambda x:x = = K,lst[start])): #checking if tuple is all K or not return False return all_k(start + 1 ,lst,K) # initializing list test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 #calling recursive function res = all_k( 0 ,test_list,K) print ( "Are all elements K ? : " + str (res)) #this code contributed by tvsk |
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
# Check if tuple list has all K # Using set() function # initializing list test_list = [( 4 , 4 ), ( 4 , 4 , 4 ), ( 4 , 4 ), ( 4 , 4 , 4 , 4 ), ( 4 , )] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 # using set() function res = False if all ( set (tup) = = {K} for tup in test_list): res = True print ( "Are all elements K ? : " + str (res)) #this code contributed by Vinay Pinjala. |
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)