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
# 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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
OutputThe 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.
OutputThe 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 and list are identical
Sometimes while working with different data in Python, we can have a problem of having data in different containers. In this situations, there can be need to test if data is identical cross containers. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This is th
7 min read
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
Python - Check if all elements in 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. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting
3 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 variable is tuple
We are given a variable, and our task is to check whether it is a tuple. For example, if the variable is (1, 2, 3), it is a tuple, so the output should be True. If the variable is not a tuple, the output should be False.Using isinstance()isinstance() function is the most common and Pythonic way to c
2 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
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
7 min read
Python - Check if element is present in tuple
We are given a tuple and our task is to find whether given element is present in tuple or not. For example x = (1, 2, 3, 4, 5) and we need to find if 3 is present in tuple so that in this case resultant output should be True.Using in Operatorin operator checks if an element is present in a tuple by
2 min read