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)
Similar Reads
Python | Check if tuple has any None value
Sometimes, while working with Python, we can have a problem in which we have a record and we need to check if it contains all valid values i.e has any None value. This kind of problem is common in data preprocessing steps. Let's discuss certain ways in which this task can be performed. Method #1 : U
5 min read
How to Check if Tuple is empty in Python ?
A Tuple is an immutable sequence, often used for grouping data. You need to check if a tuple is empty before performing operations. Checking if a tuple is empty is straightforward and can be done in multiple ways. Using the built-in len() will return the number of elements in a tuple and if the tupl
2 min read
Python | Check if two list of tuples are identical
Sometimes, while working with tuples, we can have a problem in which we have list of tuples and we need to test if they are exactly identical. This is a very basic problem and can occur in any domain. Let's discuss certain ways in which this task can be done. Method #1 : Using == operator This is th
4 min read
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
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 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
Check if Tuple Exists as Dictionary Key - Python
The task is to check if a tuple exists as a key in a dictionary. In Python, dictionaries use hash tables which provide an efficient way to check for the presence of a key. The goal is to verify if a given tuple is present as a key in the dictionary. For example, given a dictionary d = {(3, 4): 'gfg'
3 min read
Python - Test if tuple list has Single element
Given a Tuple list, check if it is composed of only one element, used multiple times. Input : test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)] Output : True Explanation : All elements are equal to 3. Input : test_list = [(3, 3, 3), (3, 3), (3, 4, 3), (3, 3)] Output : False Explanation : All elemen
8 min read
Python - Check for Sublist in List
The task of checking for a sublist in a list in Python involves determining whether a given sequence of elements (sublist) appears consecutively within a larger list. This is a common problem in programming, particularly in data processing and pattern matching. For example, if we have a list [5, 6,
4 min read
Python - Check if all tuples have element difference less than K
Given a Tuple list, check if each tuple has a difference less than K. Input : test_list = [(3, 4), (1, 2), (7, 8), (9, 13)], K = 2 Output : False Explanation : 13 - 9 = 4 > 2. Input : test_list = [(3, 4), (1, 2), (7, 8)], K = 2Output : True Explanation : All have abs. diff 1 < 2. Method #1 : U
7 min read