Python – Filter all uppercase characters Tuples from given list of tuples
Last Updated :
19 Mar, 2023
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
test_list = [( "GFG" , "IS" , "BEST" ), ( "GFg" , "AVERAGE" ),
( "GFG" , ), ( "Gfg" , "CS" )]
print ( "The original list is : " + str (test_list))
res_list = []
for sub in test_list:
res = True
for ele in sub:
if not ele.isupper():
res = False
break
if res:
res_list.append(sub)
print ( "Filtered Tuples : " + str (res_list))
|
Output
The 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
test_list = [( "GFG" , "IS" , "BEST" ), ( "GFg" , "AVERAGE" ), ( "GFG" , ), ( "Gfg" , "CS" )]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if all (ele.isupper() for ele in sub)]
print ( "Filtered Tuples : " + str (res))
|
Output
The 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
test_list = [( "GFG" , "IS" , "BEST" ), ( "GFg" , "AVERAGE" ),
( "GFG" , ), ( "Gfg" , "CS" )]
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])
print ( "Filtered Tuples : " + str (nl))
|
Output
The 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
test_list = [( "GFG" , "IS" , "BEST" ), ( "GFg" , "AVERAGE" ), ( "GFG" , ), ( "Gfg" , "CS" )]
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)
print ( "Filtered Tuples : " + str (res))
|
Output
The 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
test_list = [( "GFG" , "IS" , "BEST" ), ( "GFg" , "AVERAGE" ),
( "GFG" , ), ( "Gfg" , "CS" )]
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)
print ( "Filtered Tuples : " + str (res_list))
|
Output
The 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
test_list = [( "GFG" , "IS" , "BEST" ), ( "GFg" , "AVERAGE" ),
( "GFG" , ), ( "Gfg" , "CS" )]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda x: all (ele.isupper() for ele in x), test_list))
print ( "Filtered Tuples : " + str (res))
|
Output
The 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
import itertools
test_list = [( "GFG" , "IS" , "BEST" ), ( "GFg" , "AVERAGE" ),
( "GFG" , ), ( "Gfg" , "CS" )]
print ( "The original list is : " + str (test_list))
res = list (itertools.filterfalse( lambda x: not all (ele.isupper() for ele in x), test_list))
print ( "Filtered Tuples : " + str (res))
|
Output
The 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" )]
res_list = list ( filter ( lambda x: all ( map ( str .isupper, x)), test_list))
print ( "Filtered Tuples : " + str (res_list))
|
Output
Filtered 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
test_list = [( "GFG" , "IS" , "BEST" ), ( "GFg" , "AVERAGE" ), ( "GFG" , ), ( "Gfg" , "CS" )]
print ( "The original list is : " + str (test_list))
res = [tup for tup in test_list if all (re.match( '^[A-Z]+$' , word) for word in tup)]
print ( "Filtered Tuples : " + str (res))
|
Output
The 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 C/C++ Code # Python code to remove a
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
Python - Test if String contains any Uppercase character
The goal is to check if a given string contains at least one uppercase letter (A-Z). Using any() and isupper()any() function, combined with isdigit(), checks if any character in a string is a digit. It efficiently scans the string and returns True if at least one digit is found. [GFGTABS] Python # D
3 min read
Python - Strings with all given List characters
GIven Strings List and character list, extract all strings, having all characters from character list. Input : test_list = ["Geeks", "Gfg", "Geeksforgeeks", "free"], chr_list = [ 'f', 'r', 'e'] Output : ['free', "Geeksforgeeks"] Explanation : Only "free" and "Geeksforgeeks" contains all 'f', 'r' and
4 min read
Python program to extract characters in given range from a string list
Given a Strings List, extract characters in index range spanning entire Strings list. Input : test_list = ["geeksforgeeks", "is", "best", "for", "geeks"], strt, end = 14, 20 Output : sbest Explanation : Once concatenated, 14 - 20 range is extracted.Input : test_list = ["geeksforgeeks", "is", "best",
4 min read
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x
5 min read
Python Program to Converts Characters To Uppercase Around Numbers
Given a String, the following program converts the alphabetic character around any digit to its uppercase. Input : test_str = 'geeks4geeks is best1 f6or ge8eks' Output : geekS4Geeks is besT1 F6Or gE8Ek Explanation : S and G are uppercased as surrounded by 4.Input : test_str = 'geeks4geeks best1 f6or
8 min read