Open In App

Python – Filter all uppercase characters Tuples from given list of tuples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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))


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




# 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))


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




# 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))


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




# 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))


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




# 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))


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




# 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))


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




# 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))


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")]
 
# 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))


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
 
# 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.


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).



Last Updated : 19 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads