

- 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
Remove Tuples from the List having every element as None in Python
When it is required to remove tuples from a list of tuples where a ‘None’ element is present, a list comprehension can be used.
Below is a demonstration of the same −
Example
my_list = [(2, None, 12), (None, None, None), (23, 64), (121, 13), (None, ), (None, 45, 6)] print("The list is : ") print(my_list) my_result = [sub for sub in my_list if not all(elem == None for elem in sub)] print("The None tuples have been removed, the result is : " ) print(my_result)
Output
The list is : [(2, None, 12), (None, None, None), (23, 64), (121, 13), (None,), (None, 45, 6)] The None tuples have been removed, the result is : [(2, None, 12), (23, 64), (121, 13), (None, 45, 6)]
Explanation
A list of tuple is defined, and is displayed on the console.
The list comprehension is used to iterate over the list.
The ‘all’ condition is used to see if there are ‘None’ elements.
When ‘None’ elements are present, they are filtered out.
The remaining data is assigned to a variable.
This variable is displayed as the output.
- Related Questions & Answers
- Python – Remove Tuples from a List having every element as None
- Remove tuples having duplicate first value from given list of tuples in Python
- Remove duplicate tuples from list of tuples in Python
- Python | Remove empty tuples from a list
- Python Program to remove a specific digit from every element of the list
- Accessing nth element from Python tuples in list
- Remove tuples from list of tuples if greater than n in Python
- Find the tuples containing the given element from a list of tuples in Python
- Filter Tuples by Kth element from List in Python
- Rear element extraction from list of tuples records in Python
- Remove tuple from list of tuples if not containing any character in Python
- Python Program to Create a List of Tuples with the First Element as the Number and Second Element as the Square of the Number
- How to remove an element from a list in Python?
- Python program to create a list of tuples from the given list having the number and its cube in each tuple
- Create a list of tuples from given list having number and its cube in each tuple using Python
Advertisements