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 elements are not equal to any particular element.
Method #1: Using loop
In this, we check for all the elements and compare them with the initial element of the initial tuple in the tuple list, if any element is different, the result is flagged off.
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# Using loop
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
# checking for similar elements in list
res = True
for sub in test_list:
flag = True
for ele in sub:
# checking for element to be equal to initial element
if ele != test_list[0][0]:
flag = False
break
if not flag:
res = False
break
# printing result
print("Are all elements equal : " + str(res))
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# Using loop
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
# checking for similar elements in list
res = True
for sub in test_list:
flag = True
for ele in sub:
# checking for element to be equal to initial element
if ele != test_list[0][0]:
flag = False
break
if not flag:
res = False
break
# printing result
print("Are all elements equal : " + str(res))
Output
The original list is : [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)] Are all elements equal : True
Time complexity: O(n^2) where n is the number of sublists in the main list. The nested loop causes the time complexity to become quadratic.
Auxiliary space: O(1) as the code only uses a few variables and does not allocate any additional memory dynamically.
Method #2 : Using all() + list comprehension
In this, we perform task of checking all elements to be same using all(), list comprehension is used to perform task of iterating through all the tuples in the tuple list.
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# Using all() + list comprehension
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
# checking for single element using list comprehension
res = all([all(ele == test_list[0][0] for ele in sub) for sub in test_list])
# printing result
print("Are all elements equal : " + str(res))
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# Using all() + list comprehension
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
# checking for single element using list comprehension
res = all([all(ele == test_list[0][0] for ele in sub) for sub in test_list])
# printing result
print("Are all elements equal : " + str(res))
Output
The original list is : [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)] Are all elements equal : True
Time Complexity: O(n^2), where n is the number of sublists in the tuple list.
Auxiliary Space: O(1), as no extra space is required.
Method #3: Using len() and count()
In this we will initialize an empty list and iterate over list of tuples and add each element of tuple to empty list.If count of first element is equal to length of list, then all elements are same
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
x = []
res = False
for i in test_list:
for j in i:
x.append(j)
print()
if(x.count(x[0]) == len(x)):
res = True
# printing result
print("Are all elements equal : " + str(res))
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
x = []
res = False
for i in test_list:
for j in i:
x.append(j)
print()
if(x.count(x[0]) == len(x)):
res = True
# printing result
print("Are all elements equal : " + str(res))
Output
The original list is : [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)] Are all elements equal : True
Time Complexity: O(n^2), where n is the number of sublists in the tuple list.
Auxiliary Space: O(n), as extra space is required of size n.
Method #4 : Using extend() and count() methods
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 5), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
x = []
res = False
for i in test_list:
x.extend(list(i))
if(x.count(x[0]) == len(x)):
res = True
# printing result
print("Are all elements equal : " + str(res))
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 5), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
x = []
res = False
for i in test_list:
x.extend(list(i))
if(x.count(x[0]) == len(x)):
res = True
# printing result
print("Are all elements equal : " + str(res))
Output
The original list is : [(3, 3, 3), (3, 3), (3, 3, 5), (3, 3)] Are all elements equal : False
Method #5 : Using extend() and operator.countOf() 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 first element in the list is equal to the length of list
- If equal assign True to res
- Display res
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 5), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
x = []
res = False
for i in test_list:
x.extend(list(i))
import operator
if(operator.countOf(x,x[0]) == len(x)):
res = True
# printing result
print("Are all elements equal : " + str(res))
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 5), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
x = []
res = False
for i in test_list:
x.extend(list(i))
import operator
if(operator.countOf(x,x[0]) == len(x)):
res = True
# printing result
print("Are all elements equal : " + str(res))
Output
The original list is : [(3, 3, 3), (3, 3), (3, 3, 5), (3, 3)] Are all elements equal : False
Time Complexity : O(N)
Auxiliary Space : O(1)
Method #5 : Using extend()+len()+* operator
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 5), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
x = []
res = False
for i in test_list:
x.extend(list(i))
a = [x[0]]*len(x)
if(a == x):
res = True
# printing result
print("Are all elements equal : " + str(res))
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 5), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
x = []
res = False
for i in test_list:
x.extend(list(i))
a = [x[0]]*len(x)
if(a == x):
res = True
# printing result
print("Are all elements equal : " + str(res))
Output
The original list is : [(3, 3, 3), (3, 3), (3, 3, 5), (3, 3)] Are all elements equal : False
Method #5 : Using set()+len() methods
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
x = []
res = False
for i in test_list:
for j in i:
x.append(j)
print()
if(len(set(x)) == 1):
res = True
# printing result
print("Are all elements equal : " + str(res))
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
x = []
res = False
for i in test_list:
for j in i:
x.append(j)
print()
if(len(set(x)) == 1):
res = True
# printing result
print("Are all elements equal : " + str(res))
Output
The original list is : [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)] Are all elements equal : True
Method #6: Using filter()+list()+ lambda functions
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
K = test_list[0][0]
# Check if tuple list has all single element
# 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 equal : " + str(res))
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
K = test_list[0][0]
# Check if tuple list has all single element
# 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 equal : " + str(res))
Output
The original list is : [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)] Are all elements equal : True
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #7: Using recursion method.
# Python3 code to demonstrate working of
# Test if tuple list has Single element
#defining recursive function
def is_single(start,lst,value):
if start==len(lst): #base condition
return True
if not all(map(lambda x:x==value,lst[start])):
return False
return is_single(start+1,lst,value) #calling recursive method
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
K = test_list[0][0]
# Check if tuple list has all single element
res=is_single(0,test_list,K)
# printing result
print("Are all elements equal : " + str(res))
# Python3 code to demonstrate working of
# Test if tuple list has Single element
#defining recursive function
def is_single(start,lst,value):
if start==len(lst): #base condition
return True
if not all(map(lambda x:x==value,lst[start])):
return False
return is_single(start+1,lst,value) #calling recursive method
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
K = test_list[0][0]
# Check if tuple list has all single element
res=is_single(0,test_list,K)
# printing result
print("Are all elements equal : " + str(res))
Output
The original list is : [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)] Are all elements equal : True
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #8: Using the itertools.groupby() function
import itertools
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
res = all(len(list(group)) == 1 for key, group in itertools.groupby(test_list, lambda x: len(x)))
print("Are all elements single : " + str(res))
#This code is contributed by Vinay Pinjala.
import itertools
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
res = all(len(list(group)) == 1 for key, group in itertools.groupby(test_list, lambda x: len(x)))
print("Are all elements single : " + str(res))
#This code is contributed by Vinay Pinjala.
Output
The original list is : [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)] Are all elements single : True
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)