Python | Remove empty tuples from a list

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to remove empty tuples from a 
# list of tuples function to remove empty tuples 
# using list comprehension
def Remove(tuples):
    tuples = [t for t in tuples if t]
    return tuples
  
# Driver Code
tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'), 
          ('krishna', 'akbar', '45'), ('',''),()]
print(Remove(tuples))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python2 program to remove empty tuples
# from a list of tuples function to remove 
# empty tuples using filter
def Remove(tuples):
    tuples = filter(None, tuples)
    return tuples
  
# Driver Code
tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'), 
          ('krishna', 'akbar', '45'), ('',''),()]
print Remove(tuples)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to remove empty tuples from 
# a list of tuples function to remove empty 
# tuples using filter
def Remove(tuples):
    tuples = filter(None, tuples)
    return tuples
  
# Driver Code
tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'), 
          ('krishna', 'akbar', '45'), ('',''),()]
print (Remove(tuples))

chevron_right


Output:

<filter object at 0x7fe26eb0f3c8>


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.