Table of Contents
Introduction
In this section of python programming, we will understand the python code to remove the empty tuples or elements form the lists.
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
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).
0 Comments