
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
Example
def remove_empty(my_tuple): my_tuple = [t for t in my_tuple if t] return my_tuple my_tuple = [(), (), (''), (" " , " "), (45, 67, 35, 66, 74, 89, 100) , 'jane'] print("The tuple is : ") print(my_tuple) print("The method to remove empty tuples is being called...") my_result = remove_empty(my_tuple) print("The list of tuple after remvoing empty tuples is : ") print(my_result)
Output
The tuple is : [(), (), '', (' ', ' '), (45, 67, 35, 66, 74, 89, 100), 'jane'] The method to remove empty tuples is being called... The list of tuple after remvoing empty tuples is : [(' ', ' '), (45, 67, 35, 66, 74, 89, 100), 'jane']
Explanation
- A method named ‘remove_empty’ is defined, that takes a list of tuple as parameter.
- It iterates through the tuple and returns values only if they are non-empty.
- A list of tuple is defined, and is displayed on the console.
- The method is called by passing this list of tuple.
- This operation’s data is assigned to a variable.
- It is then displayed as output on the console.
- Related Questions & Answers
- Remove duplicate tuples from list of tuples in Python
- Remove tuples from list of tuples if greater than n in Python
- Remove tuples having duplicate first value from given list of tuples in Python
- Python – Remove Tuples from a List having every element as None
- How to remove empty strings from a list of strings in Python?
- Remove Tuples from the List having every element as None in Python
- How to remove an empty string from a list of empty strings in C#?
- Remove tuple from list of tuples if not containing any character in Python
- Remove matching tuples in Python
- Find the tuples containing the given element from a list of tuples in Python
- Accessing nth element from Python tuples in list
- Python program to Remove and print every third from list until it becomes empty?
- Filter Tuples by Kth element from List in Python
- Combining tuples in list of tuples in Python
- Python program to remove Duplicates elements from a List?
Advertisements