Program to remove empty tuples from the list using Python

by | Apr 9, 2021 | Python Programs

Home » Python » Python Programs » Program to remove empty tuples from the list using Python

Introduction

In this section of python programming, we will understand the python code to remove the empty tuples or elements form the lists.

Program to remove empty tuples from the list

Program

def remove_empty(input_tuples):

    input_tuples = [t for t in input_tuples if t]

    return input_tuples

   

input_tuples = [(), ('', ''), ('python', 'coding'), () , ('alpha', 'beta'), ('physics', 'maths'), ('', ''), ()]




print(remove_empty(input_tuples))

Output

Program to remove empty tuples from the list Output

Explanation

In the above python code we have created a tuple input_tuples with some entries. The function remove_empty will return the updated tuple after removing the empty entries by using list comprehension method.

Additional to list comprehension method we can also use filter() to filter out the desired elements, but when using filter on Python 3 and above we may not get the desired output. Python 3 will returns the generator (output is shown below).

Program to remove empty tuples from the list Explained

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author