Open In App

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

Last Updated : 19 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
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).



Similar Reads

Python - Filter Tuples with Strings of specific characters
Given a Tuple List, extract tuples, which have strings made up of certain characters. Input : test_list = [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg')], char_str = 'gfestb' Output : [('gfg', 'best'), ('fest', 'gfg')] Explanation : All tuples contain characters from char_str. Input : test_list = [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg
5 min read
Python program to find tuples which have all elements divisible by K from a list of tuples
Given a list of tuples. The task is to extract all tuples which have all elements divisible by K. Input : test_list = [(6, 24, 12), (60, 12, 6), (12, 18, 21)], K = 6 Output : [(6, 24, 12), (60, 12, 6)] Explanation : Both tuples have all elements multiple of 6. Input : test_list = [(6, 24, 12), (60, 10, 5), (12, 18, 21)], K = 5 Output : [(60, 10, 5)
7 min read
Python | Find the tuples containing the given element from a list of tuples
Given a list of tuples, the task is to find all those tuples containing the given element, say n. Examples: Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)] Output: [(11, 22), (11, 44)] Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] Output: [(14, 3), (1, 3), (3, 3)] There are multiple ways we can find the tuples contai
6 min read
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), ('Tuple2', 125), ('Tuple1', 135), ('Tuple4', 478)]
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() + isupper() In this, we iterate through the indices til
5 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 = 'gfg is best', upper_list = ['g', 'e', 'b'] Output :
7 min read
Python - Filter Tuples with All Even Elements
Given List of tuples, filter only those with all even elements. Input : test_list = [(6, 4, 2, 8), (5, 6, 7, 6), (8, 1, 2), (7, )] Output : [(6, 4, 2, 8)] Explanation : Only 1 tuple with all even elements. Input : test_list = [(6, 4, 2, 9), (5, 6, 7, 6), (8, 1, 2), (7, )] Output : [] Explanation : No tuple with all even elements. Method #1: Using l
9 min read
Python | Filter tuples according to list element presence
Sometimes, while working with records, we can have a problem in which we have to filter all the tuples from a list of tuples, which contains atleast one element from a list. This can have applications in many domains working with data. Let's discuss certain ways in which this task can be performed. Method #1: Using list comprehension Using list com
8 min read
Python - Filter Tuples by Kth element from List
Given a list of tuples, filter by Kth element presence in List. Input : test_list = [("GFg", 5, 9), ("is", 4, 3), ("best", 10, 29)], check_list = [4, 2, 3, 10], K = 2 Output : [('is', 4, 3)] Explanation : 3 is 2nd element and present in list, hence filtered tuple. Input : test_list = [("GFg", 5, 9), ("is", 4, 3), ("best", 10, 29)], check_list = [4,
5 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')] Method #1 : List comprehension This is a naive
5 min read
Practice Tags :