Python – Filter all uppercase characters from given list of tuples



When it is required to filter all the uppercase characters from a list of tuples, a simple iteration, a Boolean value, the ‘append’ method and the ‘isupper’ methods are used.

Example

Below is a demonstration of the same −

my_list = [("PYTHON", "IS", "Fun"), ("PYTHON", "COOl"), ("PYTHON", ), "ORIENTED", "OBJECT"] print("The list is : " ) print(my_list) my_result_list = [] for sub_list in my_list:    my_result = True    for element in sub_list:       if not element.isupper():          my_result = False          break    if my_result:       my_result_list.append(sub_list) print("The resultant list is : ") print(my_result_list)

Output

The list is : [('PYTHON', 'IS', 'Fun'), ('PYTHON', 'COOl'), ('PYTHON',), 'ORIENTED', 'OBJECT'] The resultant list is : [('PYTHON',), 'ORIENTED', 'OBJECT']

Explanation

  • A list of tuples is defined and is displayed on the console.

  • An empty list is defined.

  • The original list is iterated over, and a Boolean value is set to ‘True’.

  • The list is again iterated and every element is checked to belong to upper case.

  • If not, the Boolean value is set to False.

  • The control breaks out of the loop.

  • Based on the Boolean value, the element is appended to the empty list.

  • This list is displayed as output on the console.

Updated on: 2021-09-13T11:26:19+05:30

652 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements