Given a Tuple list, filter tuples that contain all uppercase characters.
Input : test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")]
Output : [('GFG', 'IS', 'BEST')]
Explanation : Only 1 tuple has all uppercase Strings.
Input : test_list = [("GFG", "iS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")]
Output : []
Explanation : No has all uppercase Strings.
Method #1: Using loop
In this, we iterate for each tuple and check if every string is uppercase, if no, that tuple is omitted from new tuple.
Python3
# Python3 code to demonstrate working of
# Filter uppercase characters Tuples
# Using loop
# initializing list
test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"),
("GFG", ), ("Gfg", "CS")]
# printing original list
print("The original list is : " + str(test_list))
res_list = []
for sub in test_list:
res = True
for ele in sub:
# checking for uppercase
if not ele.isupper():
res = False
break
if res:
res_list.append(sub)
# printing results
print("Filtered Tuples : " + str(res_list))
OutputThe original list is : [('GFG', 'IS', 'BEST'), ('GFg', 'AVERAGE'), ('GFG', ), ('Gfg', 'CS')]
Filtered Tuples : [('GFG', 'IS', 'BEST'), ('GFG', )]
Time complexity: O(n^2), where n is the total number of elements in the test_list.
Auxiliary space: O(n), where n is the total number of elements in the res_list.
Method #2 : Using list comprehension + all() + isupper()
In this, we check for all strings uppercase using all(), and list comprehension provide a compact solution to a problem. isupper() is used to check for uppercase.
Python3
# Python3 code to demonstrate working of
# Filter uppercase characters Tuples
# Using list comprehension + all() + isupper()
# initializing list
test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GFG", ), ("Gfg", "CS")]
# printing original list
print("The original list is : " + str(test_list))
# all() returns true only when all strings are uppercase
res = [sub for sub in test_list if all(ele.isupper() for ele in sub)]
# printing results
print("Filtered Tuples : " + str(res))
OutputThe original list is : [('GFG', 'IS', 'BEST'), ('GFg', 'AVERAGE'), ('GFG', ), ('Gfg', 'CS')]
Filtered Tuples : [('GFG', 'IS', 'BEST'), ('GFG', )]
Time Complexity: O(n * m), where n is the number of tuples and m is the average length of each tuple.
Auxiliary Space: O(k), where k is the number of tuples that satisfy the condition.
Method #3 : Using ord() function
Python3
# Python3 code to demonstrate working of
# Filter uppercase characters Tuples
# initializing list
test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"),
("GFG", ), ("Gfg", "CS")]
# printing original list
print("The original list is : " + str(test_list))
def fun(s):
c = 0
for i in s:
if(ord(i) in range(65, 91)):
c += 1
if(c == len(s)):
return True
return False
nl = []
for i in range(0, len(test_list)):
x = "".join(test_list[i])
if(fun(x)):
nl.append(test_list[i])
# printing results
print("Filtered Tuples : " + str(nl))
OutputThe original list is : [('GFG', 'IS', 'BEST'), ('GFg', 'AVERAGE'), ('GFG',), ('Gfg', 'CS')]
Filtered Tuples : [('GFG', 'IS', 'BEST'), ('GFG',)]
Time complexity: O(n*m), where n is the length of the test_list and m is the length of the longest string in the list.
Auxiliary space: O(k), where k is the number of tuples that satisfy the condition in the fun() function.
Method #4 : Using join() and replace() methods
Python3
# Python3 code to demonstrate working of
# Filter uppercase characters Tuples
# initializing list
test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GFG", ), ("Gfg", "CS")]
# printing original list
print("The original list is : " + str(test_list))
res=[]
upperalphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in test_list:
x="".join(i)
for j in upperalphabets:
x=x.replace(j,"")
if(len(x)==0):
res.append(i)
# printing results
print("Filtered Tuples : " + str(res))
OutputThe original list is : [('GFG', 'IS', 'BEST'), ('GFg', 'AVERAGE'), ('GFG',), ('Gfg', 'CS')]
Filtered Tuples : [('GFG', 'IS', 'BEST'), ('GFG',)]
Time complexity: O(n*m), where n is the length of the input list and m is the length of the longest string in the list.
Auxiliary space: O(k), where k is the number of tuples that satisfy the condition.
Method #5 : Using join()+isupper() methods
Python3
# Python3 code to demonstrate working of
# Filter uppercase characters Tuples
# Using loop+isupper()
# initializing list
test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"),
("GFG", ), ("Gfg", "CS")]
# printing original list
print("The original list is : " + str(test_list))
res_list = []
for sub in test_list:
x="".join(sub)
if x.isupper():
res_list.append(sub)
# printing results
print("Filtered Tuples : " + str(res_list))
OutputThe original list is : [('GFG', 'IS', 'BEST'), ('GFg', 'AVERAGE'), ('GFG',), ('Gfg', 'CS')]
Filtered Tuples : [('GFG', 'IS', 'BEST'), ('GFG',)]
Time Complexity: O(M * N), where M and N are the length of the list and the maximum size of each tuple respectively.
Auxiliary Space: O(N*K), where K is the maximum size of words in tuples.
Method #6: Using filter()+lambda() methods
This method uses the filter() function to filter the list of tuples and a lambda function to check if all elements in a tuple are uppercase.
Python3
# Python3 code to demonstrate working of
# Filter uppercase characters Tuples
# initializing list
test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"),
("GFG", ), ("Gfg", "CS")]
# printing original list
print("The original list is : " + str(test_list))
res = list(filter(lambda x: all(ele.isupper() for ele in x), test_list))
# printing results
print("Filtered Tuples : " + str(res))
OutputThe original list is : [('GFG', 'IS', 'BEST'), ('GFg', 'AVERAGE'), ('GFG',), ('Gfg', 'CS')]
Filtered Tuples : [('GFG', 'IS', 'BEST'), ('GFG',)]
Time complexity: O(n * m), where n is the number of tuples in the list and m is the maximum length of a tuple
Auxiliary space: O(k), where k is the number of tuples that satisfy the filter condition.
Method #7: Using itertools.filterfalse() method
Python3
# Python3 code to demonstrate working of
# Filter uppercase characters Tuples
import itertools
# initializing list
test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"),
("GFG", ), ("Gfg", "CS")]
# printing original list
print("The original list is : " + str(test_list))
res = list(itertools.filterfalse(lambda x: not all(ele.isupper() for ele in x), test_list))
# printing results
print("Filtered Tuples : " + str(res))
OutputThe original list is : [('GFG', 'IS', 'BEST'), ('GFg', 'AVERAGE'), ('GFG',), ('Gfg', 'CS')]
Filtered Tuples : [('GFG', 'IS', 'BEST'), ('GFG',)]
Time Complexity:O(N*N)
Auxiliary Space:O(N*N)
Method #8: Using map() and all() methods
Use the map() function to apply the isupper() method to each element of the sub-tuple, and then use the all() function to check if all elements in the sub-tuple are uppercase.
Python3
test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GFG", ), ("Gfg", "CS")]
# Use filter() to filter the list by a lambda function that checks if all elements in the sub-tuple are uppercase
res_list = list(filter(lambda x: all(map(str.isupper, x)), test_list))
# Print the filtered list
print("Filtered Tuples : " + str(res_list))
OutputFiltered Tuples : [('GFG', 'IS', 'BEST'), ('GFG',)]
Time complexity: O(nm), where n is the length of the input list and m is the maximum number of elements in a sub-tuple.
Auxiliary space: O(k), where k is the number of sub-tuples that satisfy the condition of having all elements in uppercase. This is because we create a new list to store the filtered sub-tuples.
Method #9: Using a list comprehension and the regex module:
Algorithm:
1.Initialize an empty list to store filtered tuples.
2.For each tuple in the input list:
a. Initialize a boolean variable is_uppercase to True.
b. For each string in the tuple:
i. Check if the string is uppercase by calling the isupper() method.
ii. If the string is not uppercase, set is_uppercase to False and break out of the inner loop.
c. If is_uppercase is still True after checking all the strings in the tuple, append the tuple to the filtered list.
3.Return the filtered list.
Python3
import re
# initializing list
test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GFG", ), ("Gfg", "CS")]
# printing original list
print("The original list is : " + str(test_list))
# filtering tuples containing only uppercase characters using regex
res = [tup for tup in test_list if all(re.match('^[A-Z]+$', word) for word in tup)]
# printing results
print("Filtered Tuples : " + str(res))
#This code is contributed by Jyothi pinjala.
OutputThe original list is : [('GFG', 'IS', 'BEST'), ('GFg', 'AVERAGE'), ('GFG',), ('Gfg', 'CS')]
Filtered Tuples : [('GFG', 'IS', 'BEST'), ('GFG',)]
Time Complexity:
The algorithm uses nested loops to iterate over the input list and each string in the tuples. The time complexity of the algorithm is O(nmk), where n is the number of tuples in the input list, m is the maximum number of strings in a tuple, and k is the maximum length of a string in the tuple. In the worst case, all tuples have m strings of length k that need to be checked, resulting in a time complexity of O(nmk).
Space Complexity:
The algorithm uses a filtered list to store the tuples containing only uppercase characters. The space complexity of the algorithm is O(pm), where p is the number of tuples in the filtered list and m is the maximum number of strings in a tuple. In the worst case, all tuples in the input list contain only uppercase characters, resulting in a filtered list of size n and a space complexity of O(nm).
Similar Reads
Python | Remove tuples having duplicate first value from given list of tuples Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples. Examples: Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'), (12.121, 'Tuple3'), (923232.2323, 'Tuple4')] Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]Input: [('Tuple1', 121), (
7 min read
Python - Get the indices of Uppercase characters in given string Given a String extract indices of uppercase characters. Input : test_str = 'GeeKsFoRGeeks' Output : [0, 3, 5, 7, 8] Explanation : Returns indices of uppercase characters. Input : test_str = 'GFG' Output : [0, 1, 2] Explanation : All are uppercase. Method #1 : Using list comprehension + range() + isu
5 min read
Python | Remove tuple from list of tuples if not containing any character Given a list of tuples, the task is to remove all those tuples which do not contain any character value. Example: Input: [(', ', 12), ('...', 55), ('-Geek', 115), ('Geeksfor', 115),] Output: [('-Geek', 115), ('Geeksfor', 115)] Method #1 : Using list comprehension Python3 # Python code to remove all
4 min read
Python program to uppercase the given characters Given a string and set of characters, convert all the characters that occurred from character set in string to uppercase() Input : test_str = 'gfg is best for geeks', upper_list = ['g', 'e', 'b'] Output : GfG is BEst for GEEks Explanation : Only selective characters uppercased.Input : test_str = 'gf
7 min read
Python - Remove given character from first element of Tuple Given a Tuple list, remove K character from 1st element of the Tuple being String. Input : test_list = [("GF$g!", 5), ("!i$s", 4), ("best!$", 10)], K = '$' Output : [('GFg!', 5), ('!is', 4), ('best!', 10)] Explanation : First element's strings K value removed. Input : test_list = [("GF$g!", 5), ("be
5 min read