Python | Remove empty tuples from a list

04 Jul, 2022
In this article, we will see how can we remove an empty tuple from a given list of tuples. We will find various ways, in which we can perform this task of removing tuples using various methods and ways in Python. Examples:
Input : tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'), 
                  ('krishna', 'akbar', '45'), ('',''),()]
Output : [('ram', '15', '8'), ('laxman', 'sita'), 
          ('krishna', 'akbar', '45'), ('', '')]

Input : tuples = [('','','8'), (), ('0', '00', '000'), 
                 ('birbal', '', '45'), (''), (),  ('',''),()]
Output : [('', '', '8'), ('0', '00', '000'), ('birbal', '', 
          '45'), ('', '')]
Method 1: Using the concept of List Comprehension 
Python3
# Python program to remove empty tuples from a# list of tuples function to remove empty tuples# using list comprehensiondef Remove(tuples):    tuples = [t for t in tuples if t]    return tuples # Driver Codetuples = [(), ('ram','15','8'), (), ('laxman', 'sita'),          ('krishna', 'akbar', '45'), ('',''),()]print(Remove(tuples))
Output:
[('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 
                           'akbar', '45'), ('', '')]
Method 2: Using the filter() method Using the inbuilt method filter() in Python, we can filter out the empty elements by passing the None as the parameter. This method works in both Python 2 and Python 3 and above, but the desired output is only shown in Python 2 because Python 3 returns a generator. filter() is the faster than the method of list comprehension. Let’s see what happens when we run the program in Python 2. 
Python
# Python2 program to remove empty tuples# from a list of tuples function to remove# empty tuples using filterdef Remove(tuples):    tuples = filter(None, tuples)    return tuples # Driver Codetuples = [(), ('ram','15','8'), (), ('laxman', 'sita'),          ('krishna', 'akbar', '45'), ('',''),()]print Remove(tuples)
Output:
[('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
Now let’s see what happens when we try running the program in Python 3 and above. On running the program in Python 3, as mentioned a generator is returned. 
Python3
# Python program to remove empty tuples from# a list of tuples function to remove empty# tuples using filterdef Remove(tuples):    tuples = filter(None, tuples)    return tuples # Driver Codetuples = [(), ('ram','15','8'), (), ('laxman', 'sita'),          ('krishna', 'akbar', '45'), ('',''),()]print (Remove(tuples))
Output:
<filter object at 0x7fe26eb0f3c8>
Method #3 : Using len() method
Python3
# Python program to remove empty tuples from a# list of tuples function to remove empty tuples# using len()def Remove(tuples):    for i in tuples:        if(len(i)==0):            tuples.remove(i)    return tuples# Driver Codetuples = [(), ('ram','15','8'), (), ('laxman', 'sita'),        ('krishna', 'akbar', '45'), ('',''),()]print(Remove(tuples))
[('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
kogantibhavya
Python list-programs
python-tuple
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe