Python To Remove empty tuples from a list
In this tutorial, we will see how we can remove an empty tuple from a given list of tuples in Python. The List is an ordered set of values that are always written between square brackets [ ]. A list has some values, which are called elements in it, elements can be accessed by their particular index.
Tuples are used for storing multiple items of different types in a single variable. It is a built-in data type in Python. A tuple is a collection of objects of different types separated by a comma (,). They are always enclosed by a pair of round brackets ().
An empty tuple does not contain any value and is represented as ().
We will be discussing various ways, by which we can remove empty tuples from a list. For example,
Input: [ (), ('1', '2', '3'), ('Study', 'tonight', '1'), (), ('4', '5'), (' ',' ') ]
Output: [ ('1', '2', '3'), ('Study', 'tonight', '1'), ('4', '5'), (' ',' ') ]
For removing empty tuples from a list in Python, we can follow these approaches:
- By using list comprehension
- By using filter() method
Approach 1: list comprehension
In this approach, we will be using the list comprehension method. List comprehension is simply a shorter syntax that can be used for creating a new list based on the values of an existing list. We will store only those tuples which have some values in them.
Algorithm
Follow the algorithm to understand the above-mentioned approach better.
Step 1- Define a function to remove empty tuples
Step 2- In the function, use list comprehension to store only those tuples which have some value
Step 3- Return the new list
Step 4- Declare a list and give some values
Step 5- Pass the list containing tuples in the function
Step 6- Print the value returned by it
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach. In this program, we have defined a function that will store only those tuples, which have some value with the help of list comprehension.
def removet(li):
li=[ num for num in li if num]
return li
li=[ (), ('Studytonight', '1', '2'), (), ('3', '4', '5', '6') ]
print(removet(li))
[('Studytonight', '1', '2'), ('3', '4', '5', '6')]
Approach 2: filter() Function
The built-in method filter() in Python, can be used to filter out the empty tuples by passing None as the parameter. This method works in Python 2, Python 3, and above versions, but the desired output is only shown in Python 2.
It is a faster method than list comprehension.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function to remove empty tuples
Step 2- In the function use filter() to leave empty tuples and store only those tuples which have some value in the list
Step 3- Return the new list
Step 4- Declare a list and give some values
Step 5- Pass the list containing tuples in the function
Step 6- Print the value returned by it
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach. In this program, we have defined a function that will filter the list elements and store only those tuples which have some value in them.
def removet(li):
li= filter(None, li)
return li
li=[ (), ('Studytonight'), (), ('1', '2',' '), (), ('3', '4'), ('5', '6') ]
print(removet(li))
#in Python 2
[ ('Studytonight'), ('1', '2',' '), ('3', '4'), ('5', '6') ]
#in Python 3
<filter object at 0x03DBAF58>
Conclusion
In this tutorial, we have learned how to remove empty tuples from a list. Empty tuples are the ones that don't can contain any value. We have to remove such tuples from the list. A tuple containing space as a value is not an empty tuple.