Python Exercise: Remove an empty tuple[s] from a list of tuples
Last update on January 29 2021 07:13:34 [UTC/GMT +8 hours]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
Python | Remove empty tuples from a list
When it is required to remove empty tuples from a list of tuples, a simple loop can be used.
A list can be used to store heterogeneous values [i.e data of any data type like integer, floating point, strings, and so on].
A list of tuple basically contains tuples enclosed in a list.
Below is a demonstration for the same −
Introduction
In this section of python programming, we will understand the python code to remove the empty tuples or elements form the lists.